<?php
/**
 * Created by PhpStorm.
 * User: tengwei
 * Date: 2018/11/16
 * Time: 10:06 AM
 */

namespace common\logics\bank;

use common\widgets\Request;
use common\widgets\Tool;
use phpDocumentor\Reflection\DocBlock\Tags\Var_;
use yii\base\BaseObject;
use Yii;
use yii\db\Exception;

class BankLogic extends BaseObject implements IBank {
	/** @var \common\sources\read\bank\BankAccess $RBankAccess */
	private $RBankAccess;
	/** @var \common\sources\write\bank\BankAccess $RBankAccess */
	private $WBankAccess;
	/** @var \common\sources\read\buyuser\BuyUserAccess $RBuyUserAccess */
	private $RBuyUserAccess;
	/** @var  \common\sources\read\message\MesgAccess $RMesgAccess */
	private $RMesgAccess;
	/** @var \common\sources\write\message\MesgAccess $WMesgAcdess */
	private $WMesgAcdess;
	/** @var \common\sources\read\buyuser\BuyUserAcountAccess $RBuyUserAcountAccess */
	private $RBuyUserAcountAccess;
	/** @var \common\sources\write\buyuser\BuyUserAcountAccess $WBuyUserAcountAccess */
	private $WBuyUserAcountAccess;
	/** @var \common\sources\write\buypepperrecord\BuyPepperRecordAccess $WBuyPepperRecordAccess */
	private $WBuyPepperRecordAccess;
	/** @var \common\sources\read\buypepperrecord\BuyPepperRecordAccess $RBuyPepperRecordAccess */
	private $RBuyPepperRecordAccess;
	/** @var \common\sources\read\buydepositrecord\BuyDepositRecordAccess $RBuyDepositRecordAccess */
	private $RBuyDepositRecordAccess;
	/** @var \common\sources\write\buydepositrecord\BuyDepositRecordAccess $WBuyDepositRecordAccess */
	private $WBuyDepositRecordAccess;
	/** @var \common\sources\read\buyrechargerecord\BuyRechargeRecordAccess $RBuyRechargeRecordAccess */
	private $RBuyRechargeRecordAccess;
	/** @var \common\sources\read\businessaccount\BusinessAccountAccess $RBusinessAccountAccess */
	private $RBusinessAccountAccess;
	/** @var \common\sources\write\businessacount\BusinessAcountAccess $WBusinessAcountAccess */
	private $WBusinessAcountAccess;
	/** @var \common\sources\read\businessdepositrecord\BusinessDepositRecordAccess $RBusinessDepositRecordAccess */
	private $RBusinessDepositRecordAccess;
	/** @var \common\sources\write\businessdepositrecord\BusinessDepositRecordAccess $WBusinessDepositRecordAccess */
	private $WBusinessDepositRecordAccess;
	/** @var \common\sources\read\businesspepperrecord\BusinessPepperRecordAccess $RBusinessPepperRecordAccess */
	private $RBusinessPepperRecordAccess;
	/** @var \common\sources\write\businesspepperrecord\BusinessPepperRecordAccess $WBusinessPepperRecordAccess */
	private $WBusinessPepperRecordAccess;
	/** @var \common\sources\read\businessrechargerecord\BusinessRechargeRecordAccess $RBusinessRechargeRecordAccess */
	private $RBusinessRechargeRecordAccess;
	/** @var \common\sources\read\taskorder\TaskOrderAccess $RTaskOrderAccess ** */
	private $RTaskOrderAccess;
	/** @var \common\sources\read\taskcommod\TaskCommodAccess $RTaskCommodAccess ** */
	private $RTaskCommodAccess;
	/** @var \common\sources\read\sendddcode\SendDdCodeAccess $RSendDdCodeAccess * */
	private $RSendDdCodeAccess;
	/** @var \common\sources\write\sendddcode\SendDdCodeAccess $WSendDdCodeAccess * */
	private $WSendDdCodeAccess;
	/** @var \common\sources\read\businesscash\BusinessCashAccess $RBusinessCashAccess * */
	private $RBusinessCashAccess;
	/**@var \common\sources\write\businesscash\BusinessCashAccess $WBusinessCashAccess * */
	private $WBusinessCashAccess;
	/** @var \common\sources\read\buypeppercash\BuyPepperCashAccess $RBuyPepperCashAccess * */
	private $RBuyPepperCashAccess;
	/** @var \common\sources\read\buyprincipalcash\BuyPrincipalCashAccess $RBuyPrincipalCashAccess * */
	private $RBuyPrincipalCashAccess;
	/** @var \common\sources\write\buyprincipalcash\BuyPrincipalCashAccess $WBuyPrincipalCashAccess * */
	private $WBuyPrincipalCashAccess;
	/** @var \common\sources\write\buypeppercash\BuyPepperCashAccess $WBuyPepperCashAccess * */
	private $WBuyPepperCashAccess;
	/** @var \common\sources\write\peppercashlog\BuyPepperCashLogAccess $WBuyPepperCashLogAccess * */
	private $WBuyPepperCashLogAccess;
	/** @var \common\sources\write\principalcashlog\BuyPrincipalCashLogAccess $WBuyPrincipalCashLogAccess * */
	private $WBuyPrincipalCashLogAccess;
	/** @var \common\sources\write\buscashlog\BusCashLogAccess $WBusCashLogAccess * */
	private $WBusCashLogAccess;
	/** @var \common\sources\write\taskorder\TaskOrderAccess $WTaskOrderAccess ** */
	private $WTaskOrderAccess;
	/** @var \common\sources\read\adminuser\AdminUserAccess $RAdminUserAccess ** */
	private $RAdminUserAccess;
	/** @var \common\logics\taskcommod\TaskCommodLogic $TaskCommodService ***/
	private $TaskCommodService;

	const AddBank    = '转账流水号充值';
	const ModifyBank = '用户资金日志修复';
	const TransBank  = '资金转移';

	public function init() {
		$this->RBankAccess                   = Yii::$container->get('RBankAccess');
		$this->WBankAccess                   = Yii::$container->get('WBankAccess');
		$this->RBuyUserAccess                = Yii::$container->get('RBuyUserAccess');
		$this->RMesgAccess                   = Yii::$container->get('RMesgAccess');
		$this->WMesgAcdess                   = Yii::$container->get('WMesgAccess');
		$this->RBuyUserAcountAccess          = Yii::$container->get('RBuyUserAcountAccess');
		$this->WBuyUserAcountAccess          = Yii::$container->get('WBuyUserAcountAccess');
		$this->RBusinessAccountAccess        = Yii::$container->get('RBusinessAccountAccess');
		$this->WBuyPepperRecordAccess        = Yii::$container->get('WBuyPepperRecordAccess');
		$this->RBuyPepperRecordAccess        = Yii::$container->get('RBuyPepperRecordAccess');
		$this->RBuyDepositRecordAccess       = Yii::$container->get('RBuyDepositRecordAccess');
		$this->WBuyDepositRecordAccess       = Yii::$container->get('WBuyDepositRecordAccess');
		$this->RBuyRechargeRecordAccess      = Yii::$container->get('RBuyRechargeRecordAccess');
		$this->WBusinessAcountAccess         = Yii::$container->get('WBusinessAcountAccess');
		$this->RBusinessDepositRecordAccess  = Yii::$container->get('RBusinessDepositRecordAccess');
		$this->WBusinessDepositRecordAccess  = Yii::$container->get('WBusinessDepositRecordAccess');
		$this->RBusinessPepperRecordAccess   = Yii::$container->get('RBusinessPepperRecordAccess');
		$this->WBusinessPepperRecordAccess   = Yii::$container->get('WBusinessPepperRecordAccess');
		$this->RBusinessRechargeRecordAccess = Yii::$container->get('RBusinessRechargeRecordAccess');
		$this->RTaskOrderAccess              = Yii::$container->get('RTaskOrderAccess');
		$this->RTaskCommodAccess             = Yii::$container->get('RTaskCommodAccess');
		$this->RSendDdCodeAccess             = Yii::$container->get('RSendDdCodeAccess');
		$this->WSendDdCodeAccess             = Yii::$container->get('WSendDdCodeAccess');
		$this->RBusinessCashAccess           = Yii::$container->get('RBusinessCashAccess');
		$this->WBusinessCashAccess           = Yii::$container->get("WBusinessCashAccess");
		$this->RBuyPepperCashAccess          = Yii::$container->get('RBuyPepperCashAccess');
		$this->RBuyPrincipalCashAccess       = Yii::$container->get('RBuyPrincipalCashAccess');
		$this->WBuyPrincipalCashAccess       = Yii::$container->get("WBuyPrincipalCashAccess");
		$this->WBuyPepperCashAccess          = Yii::$container->get("WBuyPepperCashAccess");
		$this->WBuyPepperCashLogAccess       = Yii::$container->get("WBuyPepperCashLogAccess");
		$this->WBuyPrincipalCashLogAccess    = Yii::$container->get("WBuyPrincipalCashLogAccess");
		$this->WBusCashLogAccess             = Yii::$container->get("WBusCashLogAccess");
		$this->WTaskOrderAccess              = Yii::$container->get("WTaskOrderAccess");
		$this->RAdminUserAccess              = Yii::$container->get("RAdminUserAccess");
		$this->TaskCommodService             = Yii::$container->get("TaskCommodService");

		parent::init();
	}

	//list
	public function getList($data) {
		if (empty($data['role'])) {
			return Tool::returnJson([], 401, "类型不能为空");
		}
		$task = [];
		if (1 == $data['role']) {//买手
			//本金排行榜
			$result = $this->RBuyUserAcountAccess->orderUserBank($data);
		}
		if (2 == $data['role']) {//商家
			//押金排行榜
			$result = $this->RBusinessAccountAccess->orderBalanceBank($data);
			//商家发布次数排行榜
			$task = $this->RTaskCommodAccess->taskOrderBy($data);
			foreach ($task as &$value) {
				$bankInfo                  = $this->RBusinessAccountAccess->getBusinessAccount($value['bus_id']);
				$userInfo                  = $this->RBuyUserAccess->getBuyUserInfo($value['bus_id']);
				$value['phone']            = isset($userInfo['phone']) ? $userInfo['phone'] : '';
				$value['ava_deposit']      = isset($bankInfo['ava_deposit']) ? $bankInfo['ava_deposit'] : '';
				$value['freezing_deposit'] = isset($bankInfo['freezing_deposit']) ? $bankInfo['freezing_deposit'] : '';
				$value['ava_pepper']       = isset($bankInfo['ava_pepper']) ? $bankInfo['ava_pepper'] : '';
				$value['freezing_golds']   = isset($bankInfo['freezing_golds']) ? $bankInfo['freezing_golds'] : '';
			}
		}
		return Tool::returnJson([
			'manyOrder' => $result,
			'taskOrder' => $task,
		], 200, "成功");
	}

	public function AddUserBank($data) {
		//增加钉钉验证码=验证
		if (empty($data['sys_code'])) {
			return Tool::returnJson([], 402, "请填写验证码");
		}
		if (empty($data['phone'])) {
			return Tool::returnJson([], 401, "操作人手机号不能为空");
		}
		$smsArr = $this->RSendDdCodeAccess->getSendCode($data);
		if (intval($smsArr['code']) !== intval($data['sys_code'])) {
			return Tool::returnJson($data = [], $code = 405, '验证码输入有误');
		}
		//删除钉钉验证码
		$this->WSendDdCodeAccess->delDdCodeById($smsArr['id']);

		if (empty($data['user_phone'])) {
			return Tool::returnJson([], 401, "会员id不能为空");
		}
		$data['id'] = $this->RBuyUserAccess->getBuyUserByPhone($data['user_phone']);
		if (!$data['id']) {
			return Tool::returnJson([], 401, "该商家不存在");
		}
		if (empty($data['money'])) {
			return Tool::returnJson([], 401, "充值金额不能为空");
		}
		if (empty($data['paycode'])) {
			return Tool::returnJson([], 401, "交易流水号不能为空");
		}
		if (empty($data['operatorId'])) {
			return Tool::returnJson([], 401, "操作人不能为空");
		}
		//根据商家流水号检测是否充值成功
		$check = $this->RBusinessRechargeRecordAccess->checkRecharge($data);
		if (!$check) {
			return Tool::returnJson([], 401, "快钱充值没有该充值记录");
		}
		if ($check['amount'] < $data['money']) {
			return Tool::returnJson([], 401, "充值金额超过实际支付金额");
		}
		$data['remark'] = self::AddBank;
		$trans          = Yii::$app->db->beginTransaction();
		try {
			//增加商家账户金额
			$businessAmount = $this->WBusinessAcountAccess->addBusinessBank($data);
			if ($businessAmount) {
				$data['total_deposit'] = isset($businessAmount->ava_deposit) ? $businessAmount->ava_deposit : 0;//结余
			}
			//增加商家押金流水记录
			$this->WBusinessDepositRecordAccess->addBalanceRecord($data);
			$trans->commit();
			return Tool::returnJson([], 200, '成功');
		} catch (\Exception $e) {
			$trans->rollBack();
			throw new Exception($e);
		}
		return Tool::returnJson([], 409, '失败');
	}

	//调整佣金/押金
	public function ChangeBank($data) {
		//增加钉钉验证码=验证
		if (empty($data['sys_code'])) {
			return Tool::returnJson([], 401, "请填写验证码");
		}
		if (empty($data['phone'])) {
			return Tool::returnJson([], 402, "操作人手机号不能为空");
		}
		$smsArr = $this->RSendDdCodeAccess->getSendCode($data);
		if (intval($smsArr['code']) !== intval($data['sys_code'])) {
			return Tool::returnJson($data = [], 403, '验证码输入有误');
		}
		//删除钉钉验证码
		$this->WSendDdCodeAccess->delDdCodeById($smsArr['id']);

		if (empty($data['user_phone'])) {
			return Tool::returnJson([], 405, "会员手机号不能为空");
		}
		if (empty($data['role'])) {
			return Tool::returnJson([], 406, "会员类型不能为空");
		}
		//检测会员是否存在
		$member = $this->RBuyUserAccess->getMemberByPhone($data);
		if (empty($member)) {
			return Tool::returnJson([], 407, "会员不存在");
		}
		$data['id'] = $member['id'];//会员id
		if (empty($data['change'])) {
			return Tool::returnJson([], 408, "调整类型不能为空");
		}
		if (empty($data['action_type'])) {
			return Tool::returnJson([], 409, "操作类型不能为空");
		}
		if (empty($data['task_code'])) {
			return Tool::returnJson([], 410, "任务编号不能为空");
		}
		//判断该会员是否与该任务有关
		if (1 == $data['role']) {//买手
//            $check = $this->RTaskOrderAccess->checkUserTask($data['task_code'],$data['id']);
//            if (!$check) {
//                return Tool::returnJson([],411,"该任务编号不存在");
//            }
//            if ($member['id'] != $check['buy_id'])
//            {
//                return Tool::returnJson([],412,"该任务与该会员不相关");
//            }
		}
		if (2 == $data['role']) {//商家
			$check = $this->RTaskCommodAccess->checkTask($data['task_code']);
//            if (!$check) {
//                return Tool::returnJson([],413,"该任务不存在");
//            }
//            if ($member['id'] != $check['bus_id'])
//            {
//                return Tool::returnJson([],415,"该任务与该商家不相关");
//            }

		}

		if (empty($data['money'])) {
			return Tool::returnJson([], 416, "调整数量不能为空");
		}
		if (empty($data['img_url'])) {
			return Tool::returnJson([], 417, "验证截图不能为空");
		}
		if (empty($data['remark'])) {
			return Tool::returnJson([], 418, "备注内容不能为空");
		}
		if (empty($data['operatorId'])) {
			return Tool::returnJson([], 419, "操作人不能为空");
		}
		$trans = Yii::$app->db->beginTransaction();
		try {
			if (1 == $data['role']) {//买手
				$buyAccount = $this->RBuyUserAcountAccess->getBuyUserAccount($member['id']);
				if ($data['action_type'] == 1)//1-充值
				{
					if (empty($buyAccount)) {
						$arr = [
							"buy_id"         => $member['id'],
							"remain_funds"   => 0,
							"remail_golds"   => number_format($data['money'], 2, ".", ""),
							"freezing_funds" => 0,
							"freezing_golds" => 0,
							"income_golds"   => 0,
						];
						$res = $this->WBuyUserAcountAccess->addUserAccount($arr);
						if (!$res) {
							$trans->rollBack();
							return Tool::returnJson([], 420, '充值失败花椒失败');
						}
					} else {
						$arr = [
							"buy_id"       => $member['id'],
							"remail_golds" => number_format($buyAccount['remail_golds'] + $data['money'], 2, ".", ""),
						];
						$res = $this->WBuyUserAcountAccess->modifyBuyUserRemailGolds($arr);
						if (!$res) {
							$trans->rollBack();
							return Tool::returnJson([], 421, '充值失败花椒失败');
						}
					}
					//增加买手充值花椒记录
					$pepper_arr = [
						"buy_id"         => $member['id'],
						"task_order_num" => '',
						"task_order_id"  => 0,
						"expend"         => 0,
						"income"         => $data['money'],
						"balance"        => number_format((isset($buyAccount['remail_golds']) ? $buyAccount['remail_golds'] : 0.00) + $data['money'], 2, ".", ""),
                        "freeze_change" => 0,
                        "freeze_balance" => $buyAccount['freezing_golds'],
						"img_url"        => $data['img_url'],
						"remark"         => $data['remark'],
						"operatorId"     => $data['phone'],
						"action_type"    => $data['action_type'],
						"type"           => 1,
					];
					$pepper_res = $this->WBuyPepperRecordAccess->addBuyPepperRecord($pepper_arr);
					if (!$pepper_res) {
						$trans->rollBack();
						return Tool::returnJson([], 422, '充值失败花椒失败');
					}
					$trans->commit();
					return Tool::returnJson([], 200, "成功");
				} else if ($data['action_type'] == 2)//2-扣减 花椒
				{
					if (empty($buyAccount)) {
						$trans->rollBack();
						return Tool::returnJson([], 422, '该买手账号中没有可用的余额');
					} else {
						$remail_golds = number_format($buyAccount['remail_golds'] - $data['money'], 2, ".", "");
						if ($remail_golds < 0) {
							$trans->rollBack();
							return Tool::returnJson([], 423, '账号可用花椒余额不足');
						}
						$arr = [
							"buy_id"       => $member['id'],
							"remail_golds" => $remail_golds,
						];
						$res = $this->WBuyUserAcountAccess->modifyBuyUserRemailGolds($arr);
						if (!$res) {
							$trans->rollBack();
							return Tool::returnJson([], 425, '扣减花椒失败');
						}
					}
					//扣减花椒记录
					//增加买手扣减花椒记录
					$pepper_arr = [
						"buy_id"         => $member['id'],
						"task_order_num" => '',
						"task_order_id"  => 0,
						"expend"         => $data['money'],
						"income"         => 0,
						"balance"        => number_format((isset($buyAccount['remail_golds']) ? $buyAccount['remail_golds'] : 0.00) - $data['money'], 2, ".", ""),
                        "freeze_change" => 0,
                        "freeze_balance" => $buyAccount['freezing_golds'],
						"img_url"        => $data['img_url'],
						"remark"         => $data['remark'],
						"operatorId"     => $data['phone'],
						"action_type"    => $data['action_type'],
						"type"           => 2,
					];
					$pepper_res = $this->WBuyPepperRecordAccess->addBuyPepperRecord($pepper_arr);
					if (!$pepper_res) {
						$trans->rollBack();
						return Tool::returnJson([], 426, '扣减花椒失败');
					}
					$trans->commit();
					return Tool::returnJson([], 200, "成功");
				} else if ($data['action_type'] == 3)//3-冻结
				{
					if (empty($buyAccount)) {
						$trans->rollBack();
						return Tool::returnJson([], 422, '该买手账号中没有可用的余额');
					} else {
						$remail_golds   = number_format($buyAccount['remail_golds'] - $data['money'], 2, ".", "");
						$freezing_golds = number_format($buyAccount['freezing_golds'] + $data['money'], 2, ".", "");
						if ($remail_golds < 0) {
							$trans->rollBack();
							return Tool::returnJson([], 423, '账号可用花椒余额不足');
						}
						$arr = [
							"buy_id"         => $member['id'],
							"remail_golds"   => $remail_golds,
							"freezing_golds" => $freezing_golds,
						];
						$res = $this->WBuyUserAcountAccess->freezingGoldsPepper($arr);
						if (!$res) {
							$trans->rollBack();
							return Tool::returnJson([], 425, '冻结花椒失败');
						}
					}
					//增加买手冻结花椒记录
					$pepper_arr = [
						"buy_id"         => $member['id'],
						"task_order_num" => '',
						"task_order_id"  => 0,
						"expend"         => $data['money'],
						"income"         => 0,
						"balance"        => $remail_golds,
                        "freeze_change" => $data['money'],
                        "freeze_balance" => $freezing_golds,
						"img_url"        => $data['img_url'],
						"remark"         => $data['remark'],
						"operatorId"     => $data['phone'],
						"action_type"    => $data['action_type'],
						"type"           => 4,
					];
					$pepper_res = $this->WBuyPepperRecordAccess->addBuyPepperRecord($pepper_arr);
					if (!$pepper_res) {
						$trans->rollBack();
						return Tool::returnJson([], 422, '冻结花椒失败');
					}
					$trans->commit();
					return Tool::returnJson([], 200, "冻结成功");
				} else if ($data['action_type'] == 4)//4-解冻
				{
					if (empty($buyAccount)) {
						$trans->rollBack();
						return Tool::returnJson([], 422, '该买手账号中没有可用的余额');
					} else {
						$remail_golds   = number_format($buyAccount['remail_golds'] + $data['money'], 2, ".", "");
						$freezing_golds = number_format($buyAccount['freezing_golds'] - $data['money'], 2, ".", "");
						if ($freezing_golds < 0) {
							$trans->rollBack();
							return Tool::returnJson([], 460, '解冻金额的大于冻结金额');
						}
						$arr = [
							"buy_id"         => $member['id'],
							"remail_golds"   => $remail_golds,
							"freezing_golds" => $freezing_golds,
						];
						$res = $this->WBuyUserAcountAccess->freezingGoldsPepper($arr);
						if (!$res) {
							$trans->rollBack();
							return Tool::returnJson([], 425, '解冻花椒失败');
						}
					}
					//增加买手冻结花椒记录
					$pepper_arr = [
						"buy_id"         => $member['id'],
						"task_order_num" => '',
						"task_order_id"  => 0,
						"expend"         => 0,
						"income"         => $data['money'],
						"balance"        => $remail_golds,
                        "freeze_change" => -$data['money'],
                        "freeze_balance" => $freezing_golds,
						"img_url"        => $data['img_url'],
						"remark"         => $data['remark'],
						"operatorId"     => $data['phone'],
						"action_type"    => $data['action_type'],
						"type"           => 5,
					];
					$pepper_res = $this->WBuyPepperRecordAccess->addBuyPepperRecord($pepper_arr);
					if (!$pepper_res) {
						$trans->rollBack();
						return Tool::returnJson([], 422, '解冻花椒失败');
					}
					$trans->commit();
					return Tool::returnJson([], 200, "解冻成功");
				} else if ($data['action_type'] == 5)//5-扣除冻结
				{
					if (empty($buyAccount)) {
						$trans->rollBack();
						return Tool::returnJson([], 422, '该买手账号中没有可用的余额');
					} else {
						$freezing_golds = number_format($buyAccount['freezing_golds'] - $data['money'], 2, ".", "");
						$arr            = [
							"buy_id"         => $member['id'],
							"freezing_golds" => $freezing_golds,
						];
						$res            = $this->WBuyUserAcountAccess->deductFreezingPepper($arr);
						if (!$res) {
							$trans->rollBack();
							return Tool::returnJson([], 425, '扣除冻结花椒失败');
						}
					}
					//增加买手冻结花椒记录
					$pepper_arr = [
						"buy_id"         => $member['id'],
						"task_order_num" => '',
						"task_order_id"  => 0,
						"expend"         => 0,
						"income"         => $data['money'],
						"balance"        => $freezing_golds,
                        "freeze_change" => -$data['money'],
                        "freeze_balance" => $freezing_golds,
						"img_url"        => $data['img_url'],
						"remark"         => $data['remark'],
						"operatorId"     => $data['phone'],
						"action_type"    => $data['action_type'],
						"type"           => 5,
					];
					$pepper_res = $this->WBuyPepperRecordAccess->addBuyPepperRecord($pepper_arr);
					if (!$pepper_res) {
						$trans->rollBack();
						return Tool::returnJson([], 426, '扣除冻结花椒失败');
					}
					$trans->commit();
					return Tool::returnJson([], 200, "扣除冻结成功");
				} else {
					$trans->rollBack();
					return Tool::returnJson([], 427, '操作类型有误');
				}
			}
			if (2 == $data['role']) {//商家
				//充值押金
				$bus_account = $this->RBusinessAccountAccess->getBusinessAccount($member['id']);
				if (1 == $data['change']) {
					if ($data['action_type'] == 1) {
						if (empty($bus_account)) {
							$arr = [
								"bus_id"        => $member['id'],
								"total_deposit" => $data['money'],
								"ava_deposit"   => $data['money'],
							];
							$res = $this->WBusinessAcountAccess->addBusinessAccount($arr);
							if (!$res) {
								$trans->rollBack();
								return Tool::returnJson([], 428, '充值押金失败');
							}
						} else {
							$arr = [
								"bus_id"           => $member['id'],
								"total_deposit"    => number_format($bus_account['total_deposit'] + $data['money'], 2, ".", ""),
								"ava_deposit"      => number_format($bus_account['ava_deposit'] + $data['money'], 2, ".", ""),
								"freezing_deposit" => number_format($bus_account['freezing_deposit'], 2, ".", ""),
								"freezing_golds"   => number_format($bus_account['freezing_deposit'], 2, ".", ""),
							];
							$res = $this->WBusinessAcountAccess->modifyBusinessAccount($arr);
							if (!$res) {
								$trans->rollBack();
								return Tool::returnJson([], 429, '充值押金失败');
							}

						}
						//添加充值押金记录
						$deposite_arr = [
							"bus_id"      => $member['id'],
							"type"        => 1,
							"expend"      => 0,
							"income"      => $data['money'],
							"balance"     => number_format($bus_account['ava_deposit'] + $data['money'], 2, ".", ""),
                            "freeze_change" => 0,
                            "freeze_balance" => number_format($bus_account['freezing_deposit'], 2, ".", ""),
							"remark"      => $data['remark'],
							"task_id"     => $data['task_code'],
							"img_url"     => $data['img_url'],
							"operatorId"  => $data['phone'],
							"action_type" => $data['action_type'],
						];
						$deposite_res = $this->WBusinessDepositRecordAccess->addDepositRecord($deposite_arr);
						if (!$deposite_res) {
							$trans->rollBack();
							return Tool::returnJson([], 422, '充值押金增加押金记录失败');
						}
						$trans->commit();
						return Tool::returnJson([], 200, "成功");
					} else if ($data['action_type'] == 2)//扣减 押金
					{
						if (empty($bus_account)) {
							$trans->rollBack();
							return Tool::returnJson([], 428, '账号信息没有可用的押金');
						} else {
							//扣减押金问题
							$ava_deposit = number_format($bus_account['ava_deposit'] - $data['money'], 2, ".", "");
							if ($ava_deposit < 0) {
								$trans->rollBack();
								return Tool::returnJson([], 429, '账号押金不足,请先充值');
							}

							$arr = [
								"bus_id"           => $member['id'],
								"total_deposit"    => number_format($bus_account['total_deposit'] - $data['money'], 2, ".", ""),
								"ava_deposit"      => number_format($bus_account['ava_deposit'] - $data['money'], 2, ".", ""),
								"freezing_deposit" => number_format($bus_account['freezing_deposit'], 2, ".", ""),
								"freezing_golds"   => number_format($bus_account['freezing_deposit'], 2, ".", ""),
							];
							$res = $this->WBusinessAcountAccess->modifyBusinessAccount($arr);
							if (!$res) {
								$trans->rollBack();
								return Tool::returnJson([], 429, '充值押金失败');
							}

						}
						//添加充值押金记录
						$deposite_arr = [
							"bus_id"      => $member['id'],
							"type"        => 2,
							"expend"      => $data['money'],
							"income"      => 0,
							"balance"     => number_format($bus_account['ava_deposit'] - $data['money'], 2, ".", ""),
                            "freeze_change" => 0,
                            "freeze_balance" => number_format($bus_account['freezing_deposit'], 2, ".", ""),
							"remark"      => $data['remark'],
							"tran_num"    => $data['task_code'],
							"img_url"     => $data['img_url'],
							"operatorId"  => $data['phone'],
							"action_type" => $data['action_type'],
						];
						$deposite_res = $this->WBusinessDepositRecordAccess->addDepositRecord($deposite_arr);
						if (!$deposite_res) {
							$trans->rollBack();
							return Tool::returnJson([], 422, '扣减押金记录失败');
						}
						$trans->commit();
						return Tool::returnJson([], 200, "成功");
					} else if ($data['action_type'] == 3)//冻结
					{
						if (empty($bus_account)) {
							$trans->rollBack();
							return Tool::returnJson([], 428, '账号信息没有可用的押金');
						} else {
							//扣减押金问题
							$ava_deposit = number_format($bus_account['ava_deposit'] - $data['money'], 2, ".", "");
							if ($ava_deposit < 0) {
								$trans->rollBack();
								return Tool::returnJson([], 429, '账号押金不足,请先充值');
							}

							$arr = [
								"bus_id"           => $member['id'],
								"total_deposit"    => number_format($bus_account['total_deposit'], 2, ".", ""),
								"ava_deposit"      => $ava_deposit,
								"freezing_deposit" => number_format($bus_account['freezing_deposit'] + $data['money'], 2, ".", ""),
								"freezing_golds"   => number_format($bus_account['freezing_golds'], 2, ".", ""),
							];
							$res = $this->WBusinessAcountAccess->modifyBusinessAccount($arr);
							if (!$res) {
								$trans->rollBack();
								return Tool::returnJson([], 429, '冻结押金失败');
							}

						}
						//添加充值押金记录
						$deposite_arr = [
							"bus_id"      => $member['id'],
							"type"        => 4,
							"expend"      => $data['money'],
							"income"      => 0,
							"balance"     => $ava_deposit,
                            "freeze_change" => $data['money'],
                            "freeze_balance" => number_format($bus_account['freezing_deposit'] + $data['money'], 2, ".", ""),
							"remark"      => $data['remark'],
							"tran_num"    => $data['task_code'],
							"img_url"     => $data['img_url'],
							"operatorId"  => $data['phone'],
							"action_type" => $data['action_type'],
						];
						$deposite_res = $this->WBusinessDepositRecordAccess->addDepositRecord($deposite_arr);
						if (!$deposite_res) {
							$trans->rollBack();
							return Tool::returnJson([], 430, '冻结押金记录失败');
						}
						$trans->commit();
						return Tool::returnJson([], 200, "成功");
					} else if ($data['action_type'] == 4)//解冻
					{
						//解冻操作
						if (empty($bus_account)) {
							$trans->rollBack();
							return Tool::returnJson([], 428, '账号信息没有可用的押金');
						} else {
							//解冻押金问题
							$ava_deposit      = number_format($bus_account['ava_deposit'] + $data['money'], 2, ".", "");
							$freezing_deposit = number_format($bus_account['freezing_deposit'] - $data['money'], 2, ".", "");
							if ($freezing_deposit < 0) {
								$trans->rollBack();
								return Tool::returnJson([], 462, '账号信息解冻金额大于冻结金额');
							}

							$arr = [
								"bus_id"           => $member['id'],
								"total_deposit"    => number_format($bus_account['total_deposit'], 2, ".", ""),
								"ava_deposit"      => $ava_deposit,
								"freezing_deposit" => number_format($bus_account['freezing_deposit'] - $data['money'], 2, ".", ""),
								"freezing_golds"   => number_format($bus_account['freezing_golds'], 2, ".", ""),
							];
							$res = $this->WBusinessAcountAccess->modifyBusinessAccount($arr);
							if (!$res) {
								$trans->rollBack();
								return Tool::returnJson([], 429, '冻结押金失败');
							}

						}
						//添加解冻押金记录
						$deposite_arr = [
							"bus_id"      => $member['id'],
							"type"        => 5,
							"expend"      => 0,
							"income"      => number_format($data['money'], 2, ".", ""),
							"balance"     => $ava_deposit,
                            "freeze_change" =>  -$data['money'],
                            "freeze_balance" => number_format($bus_account['freezing_deposit'] - $data['money'], 2, ".", ""),
							"remark"      => $data['remark'],
							"tran_num"    => $data['task_code'],
							"img_url"     => $data['img_url'],
							"operatorId"  => $data['phone'],
							"action_type" => $data['action_type'],
						];
						$deposite_res = $this->WBusinessDepositRecordAccess->addDepositRecord($deposite_arr);
						if (!$deposite_res) {
							$trans->rollBack();
							return Tool::returnJson([], 430, '解冻押金记录失败');
						}
						$trans->commit();
						return Tool::returnJson([], 200, "成功");
					} else if ($data['action_type'] == 5)//扣除冻结
					{
						//扣除冻结
						if (empty($bus_account)) {
							$trans->rollBack();
							return Tool::returnJson([], 431, '账号信息没有可用的押金');
						} else {
							//解冻押金问题
							$freezing_deposit = number_format($bus_account['freezing_deposit'] - $data['money'], 2, ".", "");
							if ($freezing_deposit < 0) {
								$trans->rollBack();
								return Tool::returnJson([], 432, '扣除解冻的押金不足');
							}
							$arr = [
								"bus_id"           => $member['id'],
								"total_deposit"    => number_format($bus_account['total_deposit'], 2, ".", ""),
								"ava_deposit"      => number_format($bus_account['ava_deposit'], 2, ".", ""),
								"freezing_deposit" => $freezing_deposit,
								"freezing_golds"   => number_format($bus_account['freezing_golds'], 2, ".", ""),
							];
							$res = $this->WBusinessAcountAccess->modifyBusinessAccount($arr);
							if (!$res) {
								$trans->rollBack();
								return Tool::returnJson([], 429, '扣减冻结押金失败');
							}

						}
						//添加扣除冻结押金记录
						$deposite_arr = [
							"bus_id"      => $member['id'],
							"type"        => 6,
							"expend"      => 0,
							"income"      => 0,
							"balance"     => $bus_account['ava_deposit'],
                            "freeze_change" => -$data['money'],
                            "freeze_balance" => $freezing_deposit,
							"remark"      => $data['remark'],
							"tran_num"    => $data['task_code'],
							"img_url"     => $data['img_url'],
							"operatorId"  => $data['phone'],
							"action_type" => $data['action_type'],
						];
						$deposite_res = $this->WBusinessDepositRecordAccess->addDepositRecord($deposite_arr);
						if (!$deposite_res) {
							$trans->rollBack();
							return Tool::returnJson([], 430, '扣减解冻押金记录失败');
						}
						$trans->commit();
						return Tool::returnJson([], 200, "成功");
					}
				}
				//充值花椒
				if (2 == $data['change']) {
					$bus_account = $this->RBusinessAccountAccess->getBusinessAccount($member['id']);
					if ($data['action_type'] == 1)//充值花椒
					{
						if (empty($bus_account)) {
							$arr = [
								"bus_id"     => $member['id'],
								"ava_pepper" => $data['money'],
							];
							$res = $this->WBusinessAcountAccess->addBusinessAccountPepper($arr);
							if (!$res) {
								$trans->rollBack();
								return Tool::returnJson([], 428, '充值花椒失败');
							}
						} else {
							$arr = [
								"bus_id"         => $member['id'],
								"total_deposit"  => number_format($bus_account['total_deposit'], 2, ".", ""),
								"ava_deposit"    => number_format($bus_account['ava_deposit'], 2, ".", ""),
								"ava_pepper"     => number_format($bus_account['ava_pepper'] + $data['money'], 2, ".", ""),
								"freezing_golds" => number_format($bus_account['freezing_golds'], 2, ".", ""),
							];
							$res = $this->WBusinessAcountAccess->modifyBusPepper($arr);
							if (!$res) {
								$trans->rollBack();
								return Tool::returnJson([], 428, '充值花椒失败');
							}
						}
						//添加商家花椒记录
						$bus_pepper_arr = [
							"bus_id"         => $member['id'],
							"task_commod_id" => 0,
							"type"           => 1,
							"task_id"        => '',
							"task_order_id"  => 0,
							"task_order_num" => '',
							"expend"         => 0,
							"income"         => number_format($data['money'], 2, ".", ""),
							"balance"        => number_format($bus_account['ava_pepper'] + $data['money'], 2, ".", ""),
                            "freeze_change" => 0,
                            "freeze_balance" => $bus_account['freezing_golds'],
							"remark"         => $data['remark'],
							"img_url"        => $data['img_url'],
							"operatorId"     => $data['phone'],
							"action_type"    => $data['action_type'],
						];
						$bus_pepper_res = $this->WBusinessPepperRecordAccess->addPeppersRecord($bus_pepper_arr);
						if (!$bus_pepper_res) {
							$trans->rollBack();
							return Tool::returnJson([], 433, "添加充值花椒失败");
						}
						$trans->commit();
						return Tool::returnJson([], 200, "成功");
					} else if ($data['action_type'] == 2)//扣减
					{
						if (empty($bus_account)) {
							$trans->rollBack();
							return Tool::returnJson([], 433, "账号中没有可用扣减的花椒");
						} else {
							$arr = [
								"bus_id"         => $member['id'],
								"total_deposit"  => number_format($bus_account['total_deposit'], 2, ".", ""),
								"ava_deposit"    => number_format($bus_account['ava_deposit'], 2, ".", ""),
								"ava_pepper"     => number_format($bus_account['ava_pepper'] - $data['money'], 2, ".", ""),
								"freezing_golds" => number_format($bus_account['freezing_golds'], 2, ".", ""),
							];
							$res = $this->WBusinessAcountAccess->modifyBusPepper($arr);
							if (!$res) {
								$trans->rollBack();
								return Tool::returnJson([], 428, '扣减花椒失败');
							}
						}
						//添加商家花椒记录
						$bus_pepper_arr = [
							"bus_id"         => $member['id'],
							"task_commod_id" => 0,
							"type"           => 2,
							"task_id"        => '',
							"task_order_id"  => 0,
							"task_order_num" => '',
							"expend"         => number_format($data['money'], 2, ".", ""),
							"income"         => 0,
							"balance"        => number_format($bus_account['ava_pepper'] - $data['money'], 2, ".", ""),
                            "freeze_change" => 0,
                            "freeze_balance" => $bus_account['freezing_golds'],
							"remark"         => $data['remark'],
							"img_url"        => $data['img_url'],
							"operatorId"     => $data['phone'],
							"action_type"    => $data['action_type'],
						];
						$bus_pepper_res = $this->WBusinessPepperRecordAccess->addPeppersRecord($bus_pepper_arr);
						if (!$bus_pepper_res) {
							$trans->rollBack();
							return Tool::returnJson([], 433, "添加扣减花椒失败");
						}
						$trans->commit();
						return Tool::returnJson([], 200, "成功");
					} else if ($data['action_type'] == 3) { //冻结
						if (empty($bus_account)) {
							$trans->rollBack();
							return Tool::returnJson([], 433, "账号中没有可用冻结的花椒");
						} else {
							$ava_pepper = number_format($bus_account['ava_pepper'] - $data['money'], 2, ".", "");
							if ($ava_pepper < 0) {
								$trans->rollBack();
								return Tool::returnJson([], 456, "冻结花椒大于可用花椒");
							}

							$arr = [
								"bus_id"         => $member['id'],
								"total_deposit"  => number_format($bus_account['total_deposit'], 2, ".", ""),
								"ava_deposit"    => number_format($bus_account['ava_deposit'], 2, ".", ""),
								"ava_pepper"     => number_format($bus_account['ava_pepper'] - $data['money'], 2, ".", ""),
								"freezing_golds" => number_format($bus_account['freezing_golds'] + $data['money'], 2, ".", ""),
							];
							$res = $this->WBusinessAcountAccess->modifyBusPepper($arr);
							if (!$res) {
								$trans->rollBack();
								return Tool::returnJson([], 428, '冻结花椒失败');
							}
						}
						//添加商家花椒记录
						$bus_pepper_arr = [
							"bus_id"         => $member['id'],
							"task_commod_id" => 0,
							"type"           => 4,
							"task_id"        => '',
							"task_order_id"  => 0,
							"task_order_num" => '',
							"expend"         => number_format($data['money'], 2, ".", ""),
							"income"         => 0,
							"balance"        => number_format($bus_account['ava_pepper'] - $data['money'], 2, ".", ""),
                            "freeze_change" => $data['money'],
                            "freeze_balance" => number_format($bus_account['freezing_golds'] + $data['money'], 2, ".", ""),
							"remark"         => $data['remark'],
							"img_url"        => $data['img_url'],
							"operatorId"     => $data['phone'],
							"action_type"    => $data['action_type'],
						];
						$bus_pepper_res = $this->WBusinessPepperRecordAccess->addPeppersRecord($bus_pepper_arr);
						if (!$bus_pepper_res) {
							$trans->rollBack();
							return Tool::returnJson([], 433, "添加冻结花椒失败");
						}
						$trans->commit();
						return Tool::returnJson([], 200, "成功");
					} else if ($data['action_type'] == 4)//解冻
					{
						if (empty($bus_account)) {
							$trans->rollBack();
							return Tool::returnJson([], 435, "账号中没有可解冻的花椒");
						} else {
							$freezing_golds = number_format($bus_account['freezing_golds'] - $data['money'], 2, ".", "");
							if ($freezing_golds < 0) {
								$trans->rollBack();
								return Tool::returnJson([], 458, "解冻花椒大于冻结花椒数量");
							}
							$arr = [
								"bus_id"         => $member['id'],
								"total_deposit"  => number_format($bus_account['total_deposit'], 2, ".", ""),
								"ava_deposit"    => number_format($bus_account['ava_deposit'], 2, ".", ""),
								"ava_pepper"     => number_format($bus_account['ava_pepper'] + $data['money'], 2, ".", ""),
								"freezing_golds" => number_format($bus_account['freezing_golds'] - $data['money'], 2, ".", ""),
							];
							$res = $this->WBusinessAcountAccess->modifyBusPepper($arr);
							if (!$res) {
								$trans->rollBack();
								return Tool::returnJson([], 436, '冻结花椒失败');
							}
						}
						//添加商家花椒记录
						$bus_pepper_arr = [
							"bus_id"         => $member['id'],
							"task_commod_id" => 0,
							"type"           => 5,
							"task_id"        => '',
							"task_order_id"  => 0,
							"task_order_num" => '',
							"expend"         => 0,
							"income"         => number_format($data['money'], 2, ".", ""),
							"balance"        => number_format($bus_account['ava_pepper'] + $data['money'], 2, ".", ""),
                            "freeze_change" => -$data['money'],
                            "freeze_balance" => number_format($bus_account['freezing_golds'] - $data['money'], 2, ".", ""),
							"remark"         => $data['remark'],
							"img_url"        => $data['img_url'],
							"operatorId"     => $data['phone'],
							"action_type"    => $data['action_type'],
						];
						$bus_pepper_res = $this->WBusinessPepperRecordAccess->addPeppersRecord($bus_pepper_arr);
						if (!$bus_pepper_res) {
							$trans->rollBack();
							return Tool::returnJson([], 437, "添加解冻花椒失败");
						}
						$trans->commit();
						return Tool::returnJson([], 200, "成功");
					} else if ($data['action_type'] == 5)//扣除冻结
					{
						if (empty($bus_account)) {
							$trans->rollBack();
							return Tool::returnJson([], 438, "账号中没有冻结的花椒");
						} else {
							$arr = [
								"bus_id"         => $member['id'],
								"total_deposit"  => number_format($bus_account['total_deposit'], 2, ".", ""),
								"ava_deposit"    => number_format($bus_account['ava_deposit'], 2, ".", ""),
								"ava_pepper"     => number_format($bus_account['ava_pepper'], 2, ".", ""),
								"freezing_golds" => number_format($bus_account['freezing_golds'] - $data['money'], 2, ".", ""),
							];
							$res = $this->WBusinessAcountAccess->modifyBusPepper($arr);
							if (!$res) {
								$trans->rollBack();
								return Tool::returnJson([], 439, '冻结花椒失败');
							}
						}
						//添加商家花椒记录
						$bus_pepper_arr = [
							"bus_id"         => $member['id'],
							"task_commod_id" => 0,
							"type"           => 4,
							"task_id"        => '',
							"task_order_id"  => 0,
							"task_order_num" => '',
							"expend"         => number_format($data['money'], 2, ".", ""),
							"income"         => 0,
							"balance"        => number_format($bus_account['ava_pepper'], 2, ".", ""),
                            "freeze_change" =>  -$data['money'],
                            "freeze_balance" => number_format($bus_account['freezing_golds'] - $data['money'], 2, ".", ""),
							"remark"         => $data['remark'],
							"img_url"        => $data['img_url'],
							"operatorId"     => $data['phone'],
							"action_type"    => $data['action_type'],
						];
						$bus_pepper_res = $this->WBusinessPepperRecordAccess->addPeppersRecord($bus_pepper_arr);
						if (!$bus_pepper_res) {
							$trans->rollBack();
							return Tool::returnJson([], 451, "添加扣除冻结花椒失败");
						}
						$trans->commit();
						return Tool::returnJson([], 200, "成功");

					}  else if ($data['action_type'] == 6) {
						//首单免佣金
						//每个商户限制一次
						$cData = [
							"bus_id"  => $member['id'],
							"type"    => 8,
						];
						$count = $this->RBusinessPepperRecordAccess->getCommissionCount($cData);
						if($count>0){
							$trans->rollBack();
							return Tool::returnJson([], 428, '同一商家不可添加多次免单');
						}

						$info = $this->RTaskCommodAccess->getTaskDetial($data['task_code']);
						if($info['bus_phone'] != $data['user_phone']){
							$trans->rollBack();
							return Tool::returnJson([], 428, '商家账户跟发布任务商户不匹配');
						}

						if (empty($bus_account)) {
							$arr = [
								"bus_id"     => $member['id'],
								"ava_pepper" => $data['money'],
							];
							$res = $this->WBusinessAcountAccess->addBusinessAccountPepper($arr);
							if (!$res) {
								$trans->rollBack();
								return Tool::returnJson([], 428, '充值花椒失败');
							}
						} else {
							$arr = [
								"bus_id"         => $member['id'],
								"total_deposit"  => number_format($bus_account['total_deposit'], 2, ".", ""),
								"ava_deposit"    => number_format($bus_account['ava_deposit'], 2, ".", ""),
								"ava_pepper"     => number_format($bus_account['ava_pepper'] + $data['money'], 2, ".", ""),
								"freezing_golds" => number_format($bus_account['freezing_golds'], 2, ".", ""),
							];
							$res = $this->WBusinessAcountAccess->modifyBusPepper($arr);
							if (!$res) {
								$trans->rollBack();
								return Tool::returnJson([], 428, '充值花椒失败');
							}
						}
						//添加商家花椒记录
						$bus_pepper_arr = [
							"bus_id"         => $member['id'],
							"task_commod_id" => 0,
							"type"           => 8,
							"task_id"        => '',
							"task_order_id"  => 0,
							"task_order_num" => '',
							"expend"         => 0,
							"income"         => number_format($data['money'], 2, ".", ""),
							"balance"        => number_format($bus_account['ava_pepper'] + $data['money'], 2, ".", ""),
                            "freeze_change" => 0,
                            "freeze_balance" => $bus_account['freezing_golds'],
							"remark"         => $data['remark'],
							"img_url"        => $data['img_url'],
							"operatorId"     => $data['phone'],
							"action_type"    => $data['action_type'],
						];
						$bus_pepper_res = $this->WBusinessPepperRecordAccess->addPeppersRecord($bus_pepper_arr);
						if (!$bus_pepper_res) {
							$trans->rollBack();
							return Tool::returnJson([], 433, "添加充值花椒失败");
						}
						$trans->commit();
						return Tool::returnJson([], 200, "成功");

					}else {
						$trans->rollBack();
						return Tool::returnJson([], 452, "添加扣除冻结花椒失败");
					}
				}
			}
		} catch (\Exception $e) {
			$trans->rollBack();
			return Tool::returnJson([], 5000, '操作失败' . $e->getMessage());
			throw new Exception($e);
		}
	}

	//  管理员调整明细
	public function ChangeList($data) {
		if (empty($data['role'])) {
			return Tool::returnJson([], 401, "角色类型不能为空！");
		}
		/**=========================== 检索条件 ================================ **/
		$where = '1=1 ';

		// search_type  高级搜索
		if (1 == $data['search_type']) {//1-手机
			$userInfo = $this->RBuyUserAccess->getBuyUser($data['search_value']);
			//买手
			if ($data['role'] == 1) {
				$where .= isset($userInfo['id']) ? ' and buy_id =' . $userInfo['id'] : ' and buy_id =0';

			} elseif ($data['role'] == 2) {
				//商家
				$where .= isset($userInfo['id']) ? ' and bus_id =' . $userInfo['id'] : ' and bus_id =0';
			}

		} elseif (2 == $data['search_type']) {//2-操作人
			$where .= ' and operatorId = ' . $data['search_value'];

		} elseif (3 == $data['search_type']) {//3-主任务编号
			$where .= ' and task_id = "' . $data['search_value'] . '"';

		} elseif (4 == $data['search_type']) {// 4-子任务编号
			$where .= ' and task_order_num = "' . $data['search_value'] . '"';

		} elseif (5 == $data['search_type']) {//5-支付编号
			$where .= ' and order_id = "' . $data['search_value'] . '"';
		}


		$rolePhone = ' 0 '; //取管理员手机号 进行检索
		//操作人 0全部 1-结算 2-非结算 3 系统

		if ($data['action_role'] == 3) {
			$where .= ' and operatorId = "system"';

		} else {
			if (0 == $data['action_role']) {
				$memberList = $this->RAdminUserAccess->getList();
			} else if (1 == $data['action_role']) {
				$memberList = $this->RAdminUserAccess->getRoleUserList(' role =4 ');
			} else  if (2 == $data['action_role']){
				$memberList = $this->RAdminUserAccess->getRoleUserList(' role !=4 ');
			}

			if ($memberList) {
				$memberList = array_column($memberList, 'username', 'phone');
				$rolePhone  = implode(',', array_keys($memberList));
			}
			if (0 == $data['action_role']) {
				$where .= ' and ( operatorId in (' . $rolePhone . ') or operatorId = "system")';
			}else{
				$where .= ' and operatorId in (' . $rolePhone . ') ';
			}
		}
		if (!empty($data['begin_time'])) {
			$where .= ' and create_time >=' . $data['begin_time'];
		}
		if (!empty($data['end_time'])) {
			$where .= ' and create_time <' . $data['end_time'];
		}
		// 操作类型
		if ($data['operate'] == 1) {
			$where .= ' and type = ' . $data['operate'];

		} elseif ($data['operate'] == 2) {
			$where .= ' and type = ' . $data['operate'];

		} elseif ($data['operate'] == 3) {
			$where .= ' and type = ' . $data['operate'];

		} elseif ($data['operate'] == 4) {
			$where .= ' and type = ' . $data['operate'];

		} elseif ($data['operate'] == 5) {
			$where .= ' and type = ' . $data['operate'];
		}

		/**=========================== 检索条件 end ================================ **/
		$list = [];
		// 排序
		$orderBy = '  create_time ' . $data['sort'];
		if ($data['role'] == 1) {
			//买手花椒
			$list = $this->RBuyPepperRecordAccess->newGetList($where, $data, $orderBy);

		} else if (($data['role'] == 2) && ($data['change'] == 1)) {
			//商家押金
			$list = $this->RBusinessDepositRecordAccess->newGetList($where, $data, $orderBy);

		} else if (($data['role'] == 2) && ($data['change'] == 2)) {
			//商家花椒
			$list = $this->RBusinessPepperRecordAccess->newGetList($where, $data, $orderBy);
		}

		if (!empty($list['list'])) {
			foreach ($list['list'] as &$value) {
				$value['create_time'] = empty($value['create_time']) ? '' : date("Y-m-d H:i:s", $value['create_time']);
				$value['update_time'] = empty($value['update_time']) ? '' : date("Y-m-d H:i:s", $value['update_time']);
				$value['task_id']     = ($value['task_id'] == '0') ? '' : $value['task_id'];

				//会员手机号
				$userId         = ($data['role'] == 1) ? $value['buy_id'] : $value['bus_id'];
				$userInfo       = $this->RBuyUserAccess->getBuyUserInfo($userId);
				$value['phone'] = isset($userInfo['phone']) ? $userInfo['phone'] : $userId;

				if ($data['action_role'] != 3) {
					//管理员信息
					$adminInfo             = $this->RAdminUserAccess->getDetial($value['operatorId']);
					$value['operatorName'] = isset($adminInfo['username']) ? $adminInfo['username'] : $value['operatorId'];
				} else {
					$value['operatorName'] = 'system';
				}
			}
			unset($value);
		}
		return Tool::returnJson($list, 200, '成功');
	}

	//第三方资金日志列表
	public function RechargeList($data) {
		$list  = [];
		$count = 0;
		if (2 == $data['change']) {//查询押金
			if (2 == $data['role']) {//商家

				$count = $this->RBusinessRechargeRecordAccess->RechargeList($data, true);
				if($count <$data['limit'])
				{
				    $data['page'] = 1;
                }
                $list  = $this->RBusinessRechargeRecordAccess->RechargeList($data);
			}
		}
		if (1 == $data['change']) {//查询花椒
			if (1 == $data['role']) {//买手
				// 买手暂无充值 TODO:: 无效

				$count = $this->RBuyUserAcountAccess->RechargeList($data, true);
                if($count <$data['limit'])
                {
                    $data['page'] = 1;
                }
                $list  = $this->RBuyUserAcountAccess->RechargeList($data);
			}
			if (2 == $data['role']) {//商家 TODO:: 无效
				$count = $this->RBusinessRechargeRecordAccess->RechargeList($data, true);
                if($count <$data['limit'])
                {
                    $data['page'] = 1;
                }
                $list  = $this->RBusinessRechargeRecordAccess->RechargeList($data);
			}
		}
		return Tool::returnJson([
			'list'  => $list,
			'page'  => $data['page'],
			'count' => $count,
		], 200, '成功');
	}

	public function ModifyUserBank($data) {
		//增加钉钉验证码=验证
		if (empty($data['sys_code'])) {
			return Tool::returnJson([], 402, "请填写验证码");
		}
		if (empty($data['phone'])) {
			return Tool::returnJson([], 401, "操作人手机号不能为空");
		}
		$smsArr = $this->RSendDdCodeAccess->getSendCode($data);
		if (intval($smsArr['code']) !== intval($data['sys_code'])) {
			return Tool::returnJson($data = [], $code = 405, '验证码输入有误');
		}
		//删除钉钉验证码
		$this->WSendDdCodeAccess->delDdCodeById($smsArr['id']);

		if (empty($data['user_phone'])) {
			return Tool::returnJson([], 401, "会员id不能为空");
		}
		$data['id'] = $this->RBuyUserAccess->getMemberByPhone($data);
		if (!$data['id']) {
			return Tool::returnJson([], 401, "该会员不存在");
		}
		if (empty($data['money'])) {
			return Tool::returnJson([], 401, "充值金额不能为空");
		}
		if (empty($data['paycode'])) {
			return Tool::returnJson([], 401, "外部跟踪编号不能为空");
		}
		if (empty($data['img_url'])) {
			return Tool::returnJson([], 401, "支付凭证不能为空");
		}
		if (empty($data['operatorId'])) {
			return Tool::returnJson([], 401, "操作人不能为空");
		}
		//根据流水号检测是否充值成功
		if (1 == $data['role']) {//买手流水表
			$check = $this->RBuyRechargeRecordAccess->checkRecharge($data);
		}
		if (2 == $data['role']) {//用户流水表
			$check = $this->RBusinessRechargeRecordAccess->checkRecharge($data);
		}
		if (!$check) {
			return Tool::returnJson([], 401, "快钱充值没有该充值记录");
		}
		if ($check['amount'] < $data['money']) {
			return Tool::returnJson([], 401, "充值金额超过实际支付金额");
		}
		$data['remark'] = self::ModifyBank;
		$trans          = Yii::$app->db->beginTransaction();
		try {
			if (1 == $data['role']) {//买手
				//增加买手账户花椒
				$userAmount = $this->WBuyUserAcountAccess->addUserBank($data);
				if ($userAmount) {
					$data['remail_golds'] = isset($userAmount->remail_golds) ? $userAmount->remail_golds : 0;
				}
				//增加买手花椒流水记录
				$this->WBuyPepperRecordAccess->addPepperRecord($data);
			}
			if (2 == $data['role']) {//商家
				//增加商家账户金额
				$businessAmount = $this->WBusinessAcountAccess->addBusinessBank($data);
				if ($businessAmount) {
					$data['total_deposit'] = isset($businessAmount->ava_deposit) ? $businessAmount->ava_deposit : 0;
				}
				//增加商家押金流水记录
				$this->WBusinessDepositRecordAccess->addBalanceRecord($data);
			}
			$trans->commit();
			return Tool::returnJson([], 200, '成功');
		} catch (\Exception $e) {
			$trans->rollBack();
//            throw new Exception($e);
		}
		return Tool::returnJson([], 409, '失败');
	}

	//商家买手账户查看
	public function ViewBank($data) {
		if (empty($data['pop_role'])) {
			return Tool::returnJson([], 401, "转出角色不能为空");
		}
		if (empty($data['pop_account'])) {
			return Tool::returnJson([], 401, "转出账户不能为空");
		}
		//判断该角色是否存在
		$memeber_1 = $this->RBuyUserAccess->checkMember($data['pop_account'], $data['pop_role']);
		if (!$memeber_1) {
			return Tool::returnJson([], 401, "转出会员不存在");
		}
		$data['pop_role_id'] = $memeber_1['id'];
		if (empty($data['push_role'])) {
			return Tool::returnJson([], 401, "转入角色不能为空");
		}
		if (empty($data['push_account'])) {
			return Tool::returnJson([], 401, "转入账户不能为空");
		}
		//判断该角色是否存在
		$memeber_2 = $this->RBuyUserAccess->checkMember($data['push_account'], $data['push_role']);
		if (!$memeber_2) {
			return Tool::returnJson([], 401, "转入会员不存在");
		}
		$data['push_role_id'] = $memeber_2['id'];

		//获取转出用户账户信息
		$pop  = [];
		$push = [];
		if (1 == $data['pop_role']) {//买手
			$pop_account = $this->RBuyUserAcountAccess->getBuyUserAccount($data['pop_role_id']);
			if (!$pop_account) {
				return Tool::returnJson([], 401, "转出账户不存在");
			}
			$pop['pop_id']  = $pop_account['buy_id'];
			$pop['pepper']  = $pop_account['remail_golds'];//可用花椒
			$pop['deposit'] = 0;//可用押金
		}
		if (2 == $data['pop_role']) {//商家
			$pop_account = $this->RBusinessAccountAccess->getBusinessAccount($data['pop_role_id']);
			if (!$pop_account) {
				return Tool::returnJson([], 401, "转出账户不存在");
			}
			$pop['pop_id']  = $pop_account['bus_id'];
			$pop['pepper']  = $pop_account['ava_pepper'];//可用花椒
			$pop['deposit'] = $pop_account['ava_deposit'];//可用押金
		}
		//获取转入用户账户信息
		if (1 == $data['push_role']) {//买手
			$push_account = $this->RBuyUserAcountAccess->getBuyUserAccount($data['push_role_id']);
			if (!$push_account) {
				$ins_data = [
					'buy_id'         => $data['push_role_id'],
					'remain_funds'   => 0,
					'remail_golds'   => 0,
					'freezing_funds' => 0,
					'freezing_golds' => 0,
					'income_golds'   => 0,
				];
				$this->WBuyUserAcountAccess->addUserAccount($ins_data);
				$push_account = $ins_data;
				//return Tool::returnJson([], 401, "转入账户(买手)不存在");
			}
			$push['push_id'] = $push_account['buy_id'];
			$push['pepper']  = $push_account['remail_golds'];
			$push['deposit'] = 0;
		}
		if (2 == $data['push_role']) {//商家
			$push_account = $this->RBusinessAccountAccess->getBusinessAccount($data['push_role_id']);
			if (!$push_account) {
				$ins_data = [
					'bus_id'        => $data['push_role_id'],
					'total_deposit' => 0,
					'ava_deposit'   => 0,
				];
				$this->WBusinessAcountAccess->addBusinessAccount($ins_data);
				$push_account = $ins_data;
				//return Tool::returnJson([], 401, "转入账户(商家)不存在");
			}
			$push['push_id'] = $push_account['bus_id'];
			$push['pepper']  = $push_account['ava_pepper'];
			$push['deposit'] = $push_account['ava_deposit'];
		}
		return Tool::returnJson([
			'pop_account'  => $pop,
			'push_account' => $push,
		], 200, '成功');
	}

	//商家买手一键转移
	public function TransferBank($data) {
        //增加钉钉验证码=验证
        if(empty($data['sys_code'])){
            return Tool::returnJson([],402,"请填写验证码");
        }
        if(empty($data['phone'])){
            return Tool::returnJson([],401,"操作人手机号不能为空");
        }
        $smsArr = $this->RSendDdCodeAccess->getSendCode($data);
        if (intval($smsArr['code']) !== intval($data['sys_code'])) {
            return Tool::returnJson($data = [], $code = 405, '验证码输入有误');
        }
        //删除钉钉验证码
        $this->WSendDdCodeAccess->delDdCodeById($smsArr['id']);

		if (empty($data['pop_role'])) {
			return Tool::returnJson([], 401, "转出角色不能为空");
		}
		if (empty($data['pop_role_id'])) {
			return Tool::returnJson([], 401, "转出账户不能为空");
		}
		if (empty($data['push_role'])) {
			return Tool::returnJson([], 401, "转入角色不能为空");
		}
		if (empty($data['push_role_id'])) {
			return Tool::returnJson([], 401, "转入账户不能为空");
		}
		if (empty($data['action_type'])) {
			return Tool::returnJson([], 401, "操作类型不能为空");
		}
		if (empty($data['task_id'])) {
			return Tool::returnJson([], 401, "任务编号不能为空");
		}
		if (empty($data['img_url'])) {
			return Tool::returnJson([], 401, "截图不能为空");
		}
		$trans = Yii::$app->db->beginTransaction();
		try {
			if (1 == $data['action_type']) {//操作类型 1-转移押金
				$result = $this->actionType_1($data);
				if (true !== $result) {
					return $result;
				}
			}
			if (2 == $data['action_type']) {//操作类型 2-转移花椒
				$result = $this->actionType_2($data);
				if (true !== $result) {
					return $result;
				}
			}
			if (3 == $data['action_type']) {//操作类型 3-转移花椒和押金
				$result = $this->actionType_3($data);
				if (true !== $result) {
					return $result;
				}
			}
			$trans->commit();
			return Tool::returnJson([], 200, "成功");
		} catch (\Exception $e) {
			$trans->rollBack();
			return Tool::returnJson([], 409, "失败" . $e->getMessage());
		}
	}

	public function actionType_1($data) {
		$businessAccount = $this->RBusinessAccountAccess->getBusinessAccount($data['pop_role_id']);//获取转出商家账户信息
		if ($data['deposit'] > $businessAccount['ava_deposit']) {
			return Tool::returnJson([], 405, '转移押金超出剩余商家押金');
		}
		$data['remark']         = self::TransBank;//备注
		$popBank                = $this->WBusinessAcountAccess->popBank($data);//转出商家押金
		$data['remail_deposit'] = $popBank->ava_deposit;//转出剩余押金
		$this->WBusinessDepositRecordAccess->addDepositRecord_pop($data);//增加转出商家押金记录
		if (2 == $data['pop_role'] && 2 == $data['push_role']) {//商家《=》商家
			$push_account         = $this->WBusinessAcountAccess->pushBank_all($data);//转入商家押金
			$data['push_deposit'] = $push_account->ava_deposit;//转入商家剩余可用押金
			$this->WBusinessDepositRecordAccess->addDepositRecord_push($data);//增加转入商家押金记录
		}
		if (2 == $data['pop_role'] && 1 == $data['push_role']) {//商家《=》买手
			$push_account         = $this->WBuyUserAcountAccess->pushBank_all($data);//转入买手押金
			$data['push_deposit'] = $push_account->remain_funds;//转入买手剩余可用本金
			$data['type'] = 1;
			$this->WBuyDepositRecordAccess->addDepositRecord_push($data);//增加转入买手本金记录
		}
		return true;
	}

	public function actionType_2($data) {
		if (1 == $data['pop_role'] && 1 == $data['push_role']) {//买手《=》买手
			//检测剩余花椒数
			$data['remail_golds'] = $this->RBuyUserAcountAccess->getPepper($data['pop_role_id'])['remail_golds'];
			if ($data['pepper'] > $data['remail_golds']) {
				return Tool::returnJson([], 405, "转移花椒数超出剩余数量");
			}
			$data['remark']       = self::TransBank;
			$pop_bank             = $this->WBuyUserAcountAccess->popBank($data);//转出买手花椒
			$data['remail_golds'] = $pop_bank->remail_golds;//转出剩余花椒
			$this->WBuyPepperRecordAccess->addPepperRecord_pop($data);//增加转出买手花椒记录
			$push_bank           = $this->WBuyUserAcountAccess->pushBank($data);//转入买手花椒
			$data['push_pepper'] = $push_bank->remail_golds;//转入剩余花椒
			$data['type'] =1 ;
			$this->WBuyPepperRecordAccess->addPepperRecord_push($data);//增加转入买手花椒记录
		}
		if (1 == $data['pop_role'] && 2 == $data['push_role']) {//买手《=》商家
			//检测剩余花椒数
			$data['remail_golds'] = $this->RBuyUserAcountAccess->getPepper($data['pop_role_id'])['remail_golds'];
			if ($data['pepper'] > $data['remail_golds']) {
				return Tool::returnJson([], 405, "转移花椒数超出剩余数量");
			}
			$data['remark']       = self::TransBank;
			$pop_bank             = $this->WBuyUserAcountAccess->popBank($data);//转出买手花椒
			$data['remail_golds'] = $pop_bank->remail_golds;//转出剩余花椒
			$this->WBuyPepperRecordAccess->addPepperRecord_pop($data);//增加买手花椒记录
			$push_bank           = $this->WBusinessAcountAccess->pushBank($data);//转入商家花椒
			$data['push_pepper'] = $push_bank->ava_pepper;//转入剩余花椒
			$this->WBusinessPepperRecordAccess->addPepperRecord_push($data);//增加转入商家花椒记录
		}
		if (2 == $data['pop_role'] && 2 == $data['push_role']) {//商家《=》商家
			$businessAccount = $this->RBusinessAccountAccess->getBusinessAccount($data['pop_role_id']);//获取转出商家账户信息
			if ($data['pepper'] > $businessAccount['ava_pepper']) {
				return Tool::returnJson([], 405, '转移花椒超出剩余商家押金');
			}
			$data['remark']       = self::TransBank;//备注
			$pop_bank             = $this->WBusinessAcountAccess->popBank($data);//转出商家花椒
			$data['remail_golds'] = $pop_bank->ava_pepper;//转出剩余花椒
			$this->WBusinessPepperRecordAccess->addPepperRecord_pop($data);//增加转出商家花椒记椒押金
			$push_account        = $this->WBusinessAcountAccess->pushBank_all($data);//转入商家花椒
			$data['push_pepper'] = $push_account->ava_pepper;//转入商家剩余可用花椒
			$this->WBusinessPepperRecordAccess->addPepperRecord_push($data);//增加转入商家花椒记录
		}
		if (2 == $data['pop_role'] && 1 == $data['push_role']) {//商家《=》买手
			$businessAccount = $this->RBusinessAccountAccess->getBusinessAccount($data['pop_role_id']);//获取转出商家账户信息
			if ($data['pepper'] > $businessAccount['ava_pepper']) {
				return Tool::returnJson([], 405, '转移花椒超出剩余商家押金');
			}
			$data['remark']       = self::TransBank;//备注
			$pop_bank             = $this->WBusinessAcountAccess->popBank($data);//转出商家花椒
			$data['remail_golds'] = $pop_bank->ava_pepper;//转出剩余花椒
			$this->WBusinessPepperRecordAccess->addPepperRecord_pop($data);//增加转出商家花椒记椒押金
			$push_bank           = $this->WBuyUserAcountAccess->pushBank($data);//转入买手花椒
			$data['push_pepper'] = $push_bank->remail_golds;//转入剩余花椒
			$this->WBuyPepperRecordAccess->addPepperRecord_push($data);//增加转入买手花椒记录
		}
		return true;
	}

	public function actionType_3($data) {
		if (2 == $data['pop_role'] && 2 == $data['push_role']) {//商家《=》商家
			$businessAccount = $this->RBusinessAccountAccess->getBusinessAccount($data['pop_role_id']);//获取转出商家账户信息
			if ($data['deposit'] > $businessAccount['ava_deposit']) {
				return Tool::returnJson([], 405, '转移押金超出剩余商家押金');
			}
			if ($data['pepper'] > $businessAccount['ava_pepper']) {
				return Tool::returnJson([], 405, '转移花椒超出剩余商家押金');
			}
			$data['remark']         = self::TransBank;//备注
			$popBank                = $this->WBusinessAcountAccess->popBank($data);//转出商家花椒押金录
			$data['remail_deposit'] = $popBank->ava_deposit;//转出剩余押金
			$data['remail_golds']   = $popBank->ava_pepper;//转出剩余花椒
			$this->WBusinessDepositRecordAccess->addDepositRecord_pop($data);//增加转出商家押金记录
			$this->WBusinessPepperRecordAccess->addPepperRecord_pop($data);//增加转出商家花椒记椒押金
			$push_account         = $this->WBusinessAcountAccess->pushBank_all($data);//转入商家
			$data['push_pepper']  = $push_account->ava_pepper;//转入商家剩余可用花椒
			$data['push_deposit'] = $push_account->ava_deposit;//转入商家剩余可用押金
			$this->WBusinessDepositRecordAccess->addDepositRecord_push($data);//增加转入商家押金记录
			$this->WBusinessPepperRecordAccess->addPepperRecord_push($data);//增加转入商家花椒记录
		}
		if (2 == $data['pop_role'] && 1 == $data['push_role']) {//商家《=》买手
			$businessAccount = $this->RBusinessAccountAccess->getBusinessAccount($data['pop_role_id']);//获取转出商家账户信息
			if ($data['deposit'] > $businessAccount['ava_deposit']) {
				return Tool::returnJson([], 405, '转移押金超出剩余商家押金');
			}
			if ($data['pepper'] > $businessAccount['ava_pepper']) {
				return Tool::returnJson([], 405, '转移花椒超出剩余商家押金');
			}
			$data['remark']         = self::TransBank;//备注
			$popBank                = $this->WBusinessAcountAccess->popBank($data);//转出商家花椒押金录
			$data['remail_deposit'] = $popBank->ava_deposit;//转出剩余押金
			$data['remail_golds']   = $popBank->ava_pepper;//转出剩余花椒
			$this->WBusinessDepositRecordAccess->addDepositRecord_pop($data);//增加转出商家押金记录
			$this->WBusinessPepperRecordAccess->addPepperRecord_pop($data);//增加转出商家花椒记椒押金
			$push_account         = $this->WBuyUserAcountAccess->pushBank_all($data);//转入买手花椒
			$data['push_pepper']  = $push_account->remail_golds;//转入买手剩余可用花椒
			$data['push_deposit'] = $push_account->remain_funds;//转入买手剩余可用本金
			$data['type'] = 1;
			$this->WBuyPepperRecordAccess->addPepperRecord_push($data);//增加转入买手花椒记录
			$this->WBuyDepositRecordAccess->addDepositRecord_push($data);//增加转入买手本金记录
		}
		return true;
	}

	//用户资金异常列表
	public function UnusualBankList($data) {
		if (1 == $data['role']) {//买手
			if (1 == $data['cash_type']) {//提现类型 1-花椒 2-本金 3-押金
				$list = $this->userPepper($data);
				return Tool::returnJson($list, 200, '成功');
			}
			if (2 == $data['cash_type']) {//提现类型 1-花椒 2-本金 3-押金
				$list = $this->userDeposit($data);
				return Tool::returnJson($list, 200, '成功');
			}
		}
		if (2 == $data['role']) {//商家
			if (1 == $data['cash_type']) {//提现类型 1-花椒 2-本金 3-押金
				$list = $this->businessPepper($data);
				return Tool::returnJson($list, 200, '成功');
			}
			if (3 == $data['cash_type']) {//提现类型 1-花椒 2-本金 3-押金
				$list = $this->businessDeposit($data);
				return Tool::returnJson($list, 200, '成功');
			}
		}
		$list1 = $this->userPepper($data);
		$list2 = $this->userDeposit($data);
		$list3 = $this->businessPepper($data);
		$list4 = $this->businessDeposit($data);
		$list5 = array_merge($list1, $list2);
		$list6 = array_merge($list3, $list4);
		$list  = array_merge($list5, $list6);
		return Tool::returnJson($list, 200, '成功');
	}

	//用户花椒异常列表
	public function userPepper($data) {
		//用户花椒异常列表
		$falseUserPepper = [];
		$buy_pepper      = $this->RBuyPepperRecordAccess->UnusualBankList($data);
		if (!empty($buy_pepper)) {
			$buy_id     = array_column($buy_pepper, 'buy_id');
			$buy_pepper = array_combine($buy_id, $buy_pepper);
			$buyUser    = $this->RBuyUserAcountAccess->allBuyUserAccount($buy_id);
			foreach ($buy_pepper as $key => &$value) {
				if (!isset($buyUser[$key]['remail_golds'])) {//不存在
					continue;
				}
				if ($value['balance'] !== $buyUser[$key]['remail_golds']) {//异常账户
					$value['remark']                = '用户花椒异常';
					$buyUser[$key]['ava_deposit']   = '';
					$buyUser[$key]['ava_pepper']    = '';
					$buyUser[$key]['total_deposit'] = '';
					//记录有错误的id
					$falseUserPepper[] = array_merge($value, $buyUser[$key]);//花椒异常用户
				}
			}
		}
		return $falseUserPepper;
	}

	//用户本金异常列表
	public function userDeposit($data) {
		//用户本金异常列表
		$falseUserRemain = [];
		$buy_deposit     = $this->RBuyDepositRecordAccess->UnusualBankList($data);
		if (!empty($buy_deposit)) {
			$buy_id_deposit  = array_column($buy_deposit, 'buy_id');
			$buy_deposit     = array_combine($buy_id_deposit, $buy_deposit);
			$buyUser_deposit = $this->RBuyUserAcountAccess->allBuyUserAccount($buy_id_deposit);
			foreach ($buy_deposit as $key3 => &$value3) {
				if (!isset($buyUser_deposit[$key3]['remain_funds'])) {//不存在
					continue;
				}
				if ($value3['balance'] !== $buyUser_deposit[$key3]['remain_funds']) {//异常账户
					$value3['remark']                        = '用户本金异常';
					$buyUser_deposit[$key3]['ava_deposit']   = '';
					$buyUser_deposit[$key3]['ava_pepper']    = '';
					$buyUser_deposit[$key3]['total_deposit'] = '';
					//记录有错误的id
					$falseUserRemain[] = array_merge($value3, $buyUser_deposit[$key3]);//花椒异常用户
				}
			}
		}
		return $falseUserRemain;
	}

	//店铺花椒异常列表
	public function businessPepper($data) {
		//店铺花椒异常列表
		$falseBusinessPepper = [];
		$business_pepper     = $this->RBusinessPepperRecordAccess->UnusualBankList($data);
		if (!empty($business_pepper)) {
			$bus_id          = array_column($business_pepper, 'bus_id');
			$business_pepper = array_combine($bus_id, $business_pepper);
			$businessUser    = $this->RBusinessAccountAccess->allBuyUserAccount($bus_id);
			foreach ($business_pepper as $key1 => &$value1) {
				if (!isset($businessUser[$key1]['ava_pepper'])) {//不存在
					continue;
				}
				if ($value1['balance'] !== $businessUser[$key1]['ava_pepper']) {//花椒异常商家
					$value1['remark']                      = '商家花椒异常';
					$businessUser[$key1]['remain_funds']   = '';
					$businessUser[$key1]['remail_golds']   = '';
					$businessUser[$key1]['freezing_funds'] = '';
					//记录有错误的id
					$falseBusinessPepper[] = array_merge($value1, $businessUser[$key1]);//花椒异常用户
				}
			}
		}
		return $falseBusinessPepper;
	}

	public function businessDeposit($data) {
		//店铺押金异常列表
		$falseBusinessDeposit = [];
		$business_deposit     = $this->RBusinessDepositRecordAccess->UnusualBankList($data);
		if (!empty($business_deposit)) {
			$bus_id_deposit       = array_column($business_deposit, 'bus_id');
			$business_deposit     = array_combine($bus_id_deposit, $business_deposit);
			$businessUser_deposit = $this->RBusinessAccountAccess->allBuyUserAccount($bus_id_deposit);
			foreach ($business_deposit as $key2 => &$value2) {
				if (!isset($businessUser_deposit[$key2]['ava_deposit'])) {//不存在
					continue;
				}
				if ($value2['balance'] !== $businessUser_deposit[$key2]['ava_deposit']) {//押金异常商家
					$value2['remark']                              = '商家押金异常';
					$businessUser_deposit[$key2]['remain_funds']   = '';
					$businessUser_deposit[$key2]['remail_golds']   = '';
					$businessUser_deposit[$key2]['freezing_funds'] = '';
					//记录有错误的id
					$falseBusinessDeposit[] = array_merge($value2, $businessUser_deposit[$key2]);//花椒异常用户
				}
			}
		}
		return $falseBusinessDeposit;
	}

	//提现审核
	public function CashAuditList($data) {
		if (empty($data['role'])) {
			return Tool::returnJson([], 401, '角色不能为空');
		}
		if (empty($data['cash_type'])) {
			return Tool::returnJson([], 402, '提现类型不能为空');
		}
		$list = [];
		if (1 == $data['role'] && 1 == $data['cash_type']) {
			//买手花椒提现列表
			$list = $this->RBuyPepperCashAccess->CashAuditList($data);

		} else if (1 == $data['role'] && 2 == $data['cash_type']) {
			//买手本金提现列表
			$list = $this->RBuyPrincipalCashAccess->CashAuditList($data);

		} else if (2 == $data['role'] && 2 == $data['cash_type']) {
			//商家本金提现列表
			$list = $this->RBusinessCashAccess->CashAuditList($data);

		} else {
			return Tool::returnJson([], 403, '提现类型有误');
		}
		if (!$list) {
			return Tool::returnJson([], 200, '成功');
		}
		foreach ($list as &$value) {
			$value['create_time']  = empty($value['create_time']) ? '' : date("Y-m-d H:i:s", $value['create_time']);
			$value['auditor_time'] = empty($value['auditor_time']) ? '' : date("Y-m-d H:i:s", $value['auditor_time']);
			$value['end_time']     = empty($value['end_time']) ? '' : date("Y-m-d H:i:s", $value['end_time']);
		}
		unset($value);
		return Tool::returnJson($list, 200, '成功');
	}

	//提现详情
	public function CashDetail($data) {
		if (empty($data['cashNo'])) {
			return Tool::returnJson([], 401, '提现单号不能为空');
		}
		if (empty($data['role'])) {
			return Tool::returnJson([], 402, '用户类型不能为空');
		}
		if (empty($data['cash_type'])) {
			return Tool::returnJson([], 403, '提现类型不能为空');
		}
		$list = [];
		if (1 == $data['role'] && 1 == $data['cash_type']) {//买手花椒提现详情
			$list = $this->RBuyPepperCashAccess->CashDetail($data);
		} else if (1 == $data['role'] && 2 == $data['cash_type']) {//买手本金提现详情
			$list = $this->RBuyPrincipalCashAccess->CashDetail($data);
//            $orderDetail = $this->RTaskOrderAccess->GetTaskSubNo();
		} else if (2 == $data['role'] && 2 == $data['cash_type']) {//商家花椒提现详情
			$list = $this->RBusinessCashAccess->CashDetail($data);
		} else {
			return Tool::returnJson([], 405, '提现类型有误');
		}

		if (!empty($list)) {
			if ($data['role'] == 1) {
				$list['status_name'] = isset(Yii::$app->params['withdrawal_cash_status'][$list['status']]) ? Yii::$app->params['withdrawal_cash_status'][$list['status']] : '';
			} else {
				$list['status_name'] = isset(Yii::$app->params['withdrawal_cash_status'][$list['cash_status']]) ? Yii::$app->params['withdrawal_cash_status'][$list['cash_status']] : '';
			}
		}

		return Tool::returnJson(["list" => $list, "cash_type" => $data['cash_type']], 200, '成功');
	}

	//修改订单备注
	public function ModifyRemark($data) {
		if (empty($data['id'])) {
			return Tool::returnJson([], 409, '提现单号不能为空');
		}
		if (empty($data['remark'])) {
			return Tool::returnJson([], 409, '备注不能为空');
		}
		if (empty($data['role'])) {
			return Tool::returnJson([], 409, '用户类型不能为空');
		}
		if (empty($data['cash_type'])) {
			return Tool::returnJson([], 409, '提现类型不能为空');
		}
		if (1 == $data['role'] && 1 == $data['cash_type']) {//买手花椒提现修改备注
			$result = $this->RBuyPepperCashAccess->ModifyRemark($data);
		} else if (1 == $data['role'] && 2 == $data['cash_type']) {//买手本金提现修改备注
			$result = $this->RBuyPrincipalCashAccess->ModifyRemark($data);
		} else if (2 == $data['role'] && 1 == $data['cash_type']) {//商家花椒提现修改备注
			//$result = $this->WBusinessCashAccess->editCashRecordRemark($data);
		} else if (2 == $data['role'] && 2 == $data['cash_type']) {//商家本金提现修改备注
			$result = $this->WBusinessCashAccess->editCashRecordRemark($data);
		}
		if ($result) {
			return Tool::returnJson([], 200, '成功');
		}
		return Tool::returnJson([], 409, '失败');
	}

	public function AuditCash($data) {
		if (empty($data['id'])) {
			return Tool::returnJson([], 401, '提现单号不能为空');
		}
		if (empty($data['status'])) {
			return Tool::returnJson([], 402, '状态不能为空');
		}
		if (empty($data['role'])) {
			return Tool::returnJson([], 403, '用户类型不能为空');
		}
		if (empty($data['cash_type'])) {
			return Tool::returnJson([], 405, '提现类型不能为空');
		}
		if (empty($data['operator'])) {
			return Tool::returnJson([], 406, '操作人不能为空');
		}

		$transaction = Yii::$app->db->beginTransaction();
		try {
			if (1 == $data['role'] && 1 == $data['cash_type']) {//买手花椒提现修改备注
				//查询提现信息是否存在
				$pepper_cash_record = $this->RBuyPepperCashAccess->getCashDetail($data['id']);
				if (empty($pepper_cash_record)) {
					$transaction->rollBack();
					return Tool::returnJson([], 407, '提现信息不存在');
				}
				if ($pepper_cash_record['status'] != 1) {
					$transaction->rollBack();
					return Tool::returnJson([], 408, '非待审核提现花椒记录不能进行操作');
				}
				$buyUser = $this->RBuyUserAccess->getBuyUserInfo($pepper_cash_record['buy_id']);
				if (empty($buyUser)) {
					$transaction->rollBack();
					return Tool::returnJson([], 409, '提现信息对应的账号信息不存在');
				}
				$buyAccount = $this->RBuyUserAcountAccess->getBuyUserAccount($pepper_cash_record['buy_id']);
				if (empty($buyAccount)) {
					$transaction->rollBack();
					return Tool::returnJson([], 410, '买手账号信息异常');
				}
				//$res = $this->WBuyPepperCashAccess->modifyPepperCash($data);
				$res = $this->WBuyPepperCashAccess->newModifyPepperCash1($data);
				if (!$res) {
					$transaction->rollBack();
					return Tool::returnJson([], 411, '审核不通过');
				}
				if ($data['status'] == 2)//审核不通过
				{
					$buy_arr = [
						"remail_golds"   => number_format($buyAccount['remail_golds'] + $pepper_cash_record['money'], 2, ".", ""),
						"freezing_golds" => number_format($buyAccount['freezing_golds'] - $pepper_cash_record['money'], 2, ".", ""),
						"buy_id"         => $buyAccount['buy_id'],
					];
					//回退冻结花椒
					$buy_res = $this->WBuyUserAcountAccess->freezingGoldsPepper($buy_arr);
					if (!$buy_res) {
						$transaction->rollBack();
						return Tool::returnJson([], 412, '返还冻结花椒失败');
					}
					//增加花椒记录
					$pepper_arra = [
						"buy_id"       => $pepper_cash_record['buy_id'],
						"expend"       => 0,
						"income"       => $pepper_cash_record['money'],
						"balance"      => number_format($buyAccount['remail_golds'] + $pepper_cash_record['money'], 2, ".", ""),
                        "freeze_change" =>-$pepper_cash_record['money'],
                        "freeze_balance" => number_format($buyAccount['freezing_golds'] - $pepper_cash_record['money'], 2, ".", ""),
						"remark"       => "买手提现花椒审核未通过返还花椒",
						"operatorId"   => $data['operator'],
						"type"         => 3,
						"auditor_user" => $data['operator'],
					];
					$res_pepper  = $this->WBuyPepperRecordAccess->addBuyPepperRecord($pepper_arra);
					if (!$res_pepper) {
						$transaction->rollBack();
						return Tool::returnJson([], 413, "增加花椒退还记录失败");
					}
				}
				$transaction->commit();
				return Tool::returnJson([], 200, "成功");
			}
			if (1 == $data['role'] && 2 == $data['cash_type']) {//买手本金提现修改备注

				$buy_principal_cash = $this->RBuyPrincipalCashAccess->getBuyPrincipalCash($data['id']);
				if (empty($buy_principal_cash)) {
					$transaction->rollBack();
					return Tool::returnJson([], 415, "买手本金记录不能为空");
				}
				if ($buy_principal_cash['status'] != 1) {
					$transaction->rollBack();
					return Tool::returnJson([], 416, "非待审核记录不能进行该操作");
				}
				$buyUser = $this->RBuyUserAccess->getBuyUserInfo($buy_principal_cash['buy_id']);
				if (empty($buyUser)) {
					$transaction->rollBack();
					return Tool::returnJson([], 409, '提现信息对应的账号信息不存在');
				}
				$buyAccount = $this->RBuyUserAcountAccess->getBuyUserAccount($buy_principal_cash['buy_id']);
				if (empty($buyAccount)) {
					$transaction->rollBack();
					return Tool::returnJson([], 410, '买手账号信息异常');
				}
				//$res = $this->WBuyPrincipalCashAccess->modifyPepperCash($data);
				$res = $this->WBuyPrincipalCashAccess->newModifyPepperCash1($data);
				if (!$res) {
					$transaction->rollBack();
					return Tool::returnJson([], 416, "非待审核记录不能进行该操作");
				}
				if ($data['status'] == 2)//审核未通过
				{
					$buy_arr = [
						"remain_funds"   => number_format($buyAccount['remain_funds'] + $buy_principal_cash['money'], 2, ".", ""),
						"remail_golds"   => $buyAccount['remail_golds'],
						"freezing_funds" => number_format($buyAccount['freezing_funds'] - $buy_principal_cash['money'], 2, ".", ""),
						"freezing_golds" => $buyAccount['freezing_golds'],
						"buy_id"         => $buyAccount['buy_id'],
					];
					//回退冻结花椒
					$buy_res = $this->WBuyUserAcountAccess->modifyBuyUserAccount($buy_arr);
					if (!$buy_res) {
						$transaction->rollBack();
						return Tool::returnJson([], 417, '返还冻结本金失败');
					}
					//增加本金记录
					$principal_arr = [
						"buy_id"       => $buy_principal_cash['buy_id'],
						"expend"       => 0,
						"income"       => $buy_principal_cash['money'],
						"balance"      => number_format($buyAccount['remail_golds'] + $buy_principal_cash['money'], 2, ".", ""),
                        "freeze_change" => -$buy_principal_cash['money'],
                        "freeze_balance" => number_format($buyAccount['freezing_funds'] - $buy_principal_cash['money'], 2, ".", ""),
						"remark"       => "买手提现本金审核未通过返还本金",
						"operatorId"   => $data['operator'],
						'type'         => 3,
						"auditor_user" => $data['operator'],
					];
					$res_principal = $this->WBuyDepositRecordAccess->addBuyDepositRecord($principal_arr);
					if (!$res_principal) {
						$transaction->rollBack();
						return Tool::returnJson([], 418, "增加本金退还记录失败");
					}
					//买手子订单状态回退
					$task_order_res = $this->WTaskOrderAccess->TaskOrderCancelStatus(['task_order_ids' => explode(",", $buy_principal_cash['task_order_ids']), "order_status" => 8]);
					if (!$task_order_res) {
						$transaction->rollBack();
						return Tool::returnJson([], 4333, "子任务订单状态回退失败");
					}
				}
				$transaction->commit();
				return Tool::returnJson([], 200, "成功");
			}
			if (2 == $data['role'] && 2 == $data['cash_type'])//商家本金提现修改备注
			{

				$business_cash_record = $this->RBusinessCashAccess->getBusinessCashRecord($data['id']);
				if (empty($business_cash_record)) {
					$transaction->rollBack();
					return Tool::returnJson([], 419, "商家本金提现记录不存在");
				}

				if ($business_cash_record['cash_status'] != 1) {
					$transaction->rollBack();
					return Tool::returnJson([], 420, "商家提现记录该状态下不能审核");
				}
				$buyUser = $this->RBuyUserAccess->getBuyUserInfo($business_cash_record['bus_id']);
				if (empty($buyUser)) {
					$transaction->rollBack();
					return Tool::returnJson([], 421, "商家账号不存在");
				}
				$bus_account = $this->RBusinessAccountAccess->getBusinessAccount($business_cash_record['bus_id']);
				if (empty($bus_account)) {
					$transaction->rollBack();
					return Tool::returnJson([], 422, "商家账号信息不存在");
				}

				if ($data['status'] == 2)//审核未通过
				{
					$bus_arr = [
						"total_deposit"    => number_format($bus_account['total_deposit'], 2, ".", ""),
						"ava_deposit"      => number_format($bus_account['ava_deposit'] + $business_cash_record['money'], 2, ".", ""),
						"freezing_deposit" => number_format($bus_account['freezing_deposit'], 2, ".", ""),
						"freezing_golds"   => $bus_account['freezing_golds'],
						"ava_pepper"       => $bus_account['ava_pepper'],
						"cash_deposit"     => number_format($bus_account['cash_deposit'] - $business_cash_record['money'], 2, ".", ""),
						"bus_id"           => $bus_account['bus_id'],
					];
					$bus_res = $this->WBusinessAcountAccess->modifyBusAccountCash($bus_arr);
					if (!$bus_res) {
						$transaction->rollBack();
						return Tool::returnJson([], 425, '返还冻结本金提现失败');
					}
					//增加本金记录
					$deposit_arr = [
						"bus_id"       => $bus_account['bus_id'],
						"type"         => 3,
						"expend"       => 0,
						"income"       => $business_cash_record['money'],
						"balance"      => number_format($bus_account['ava_deposit'] + $business_cash_record['money'], 2, ".", ""),
                        "freeze_change" => -$business_cash_record['money'],
                        "freeze_balance" =>number_format($bus_account['cash_deposit'] - $business_cash_record['money'], 2, ".", ""),
						"remark"       => "审核商家提现不通过返还本金",
						"operatorId"   => $data['operator'],
						"auditor_user" => $data['operator'],
					];
					$deposit_res = $this->WBusinessDepositRecordAccess->addDepositRecord($deposit_arr);
					if (!$deposit_res) {
						$transaction->rollBack();
						return Tool::returnJson([], 426, "返还本金记录失败");
					}
				}

				//$res = $this->WBusinessCashAccess->modifyCashRecord($data);
				$res = $this->WBusinessCashAccess->auditData($data);
				if (!$res) {
					$transaction->rollBack();
					return Tool::returnJson([], 423, "修改商家本金提现记录失败");
				}
				$transaction->commit();
				return Tool::returnJson([], 200, "成功");
			}
		} catch (\Exception $e) {
			$transaction->rollBack();
			return Tool::returnJson([], 427, "操作异常" . $e->getMessage());
		}
	}

	// 提现成功、失败
	public function modifyCash($data) {
		if (empty($data['id'])) {
			return Tool::returnJson([], 401, '提现id不能为空');
		}
		if (empty($data['status'])) {
			return Tool::returnJson([], 402, '状态不能为空');
		}
		if (empty($data['role'])) {
			return Tool::returnJson([], 403, '用户类型不能为空');
		}
		if (empty($data['cash_type'])) {
			return Tool::returnJson([], 405, '提现类型不能为空');
		}
		if (empty($data['operator'])) {
			return Tool::returnJson([], 406, "操作人不能为空");
		}
		$transaction = Yii::$app->db->beginTransaction();
		try {
			//买手花椒提现修改备注
			if (1 == $data['role'] && 1 == $data['cash_type']) {
				//查询提现信息是否存在
				$pepper_cash_record = $this->RBuyPepperCashAccess->getCashDetail($data['id']);
				if (empty($pepper_cash_record)) {
					$transaction->rollBack();
					return Tool::returnJson([], 410, '提现信息不存在');
				}
				if ($pepper_cash_record['status'] != 3) {
					$transaction->rollBack();
					return Tool::returnJson([], 460, '不能提现花椒记录');
				}
				$buyUser = $this->RBuyUserAccess->getBuyUserInfo($pepper_cash_record['buy_id']);
				if (empty($buyUser)) {
					$transaction->rollBack();
					return Tool::returnJson([], 411, '提现信息对应的账号信息不存在');
				}
				$buyAccount = $this->RBuyUserAcountAccess->getBuyUserAccount($pepper_cash_record['buy_id']);
				if (empty($buyAccount)) {
					$transaction->rollBack();
					return Tool::returnJson([], 412, '买手账号信息异常');
				}

				if ($data['status'] == 4)//提现成功
				{
					if (empty($data['kq_serial_number'])) {
						$transaction->rollBack();
						return Tool::returnJson([], 401, '交易号不能为空');
					}

					//$result = $this->WBuyPepperCashAccess->modifyPepperCash($data);
					$result = $this->WBuyPepperCashAccess->newModifyPepperCash($data);
					if ($result) {
						//修改账号资金
						$pepperCash = number_format($buyAccount['freezing_golds'] - $pepper_cash_record['money'], 2, ".", "");
						if ($pepperCash < 0) {
							$transaction->rollBack();
							return Tool::returnJson([], 488, "买手花椒冻结记录有误");
						}
						$buy_arr = [
							"freezing_golds" => number_format($buyAccount['freezing_golds'] - $pepper_cash_record['money'], 2, ".", ""),//在冻结花椒中减去花椒数量
							"buy_id"         => $pepper_cash_record['buy_id'],
						];
						$buy_res = $this->WBuyUserAcountAccess->deductFreezingPepper($buy_arr);
						if (!$buy_res) {
							$transaction->rollBack();
							return Tool::returnJson([], 412, '修改买手金额失败');
						}
						//增加买手提现成功记录
						$buy_cash_arr = [
							"buy_id"        => $pepper_cash_record['buy_id'],
							"serial_num"    => $pepper_cash_record['cash_num'],
							"money"         => $pepper_cash_record['money'],
							"arrival_money" => $pepper_cash_record['cash_money'],
							"account_num"   => $pepper_cash_record['account_num'],
							"username"      => $pepper_cash_record['real_name'],
							"status"        => 1,
							"remark"        => "买手提现花椒成功",
							"operator"      => $data['operator'],
						];
						$cash_res     = $this->WBuyPepperCashLogAccess->addBuyPepperCashLog($buy_cash_arr);
						if (!$cash_res) {
							$transaction->rollBack();
							return Tool::returnJson([], 413, "提现花椒失败");

						}
						$transaction->commit();
						return Tool::returnJson([], 200, "成功");
					}
					$transaction->rollBack();
					return Tool::returnJson([], 415, "提现花椒失败");

				} else if ($data['status'] == 5)//返款失败
				{
					$result = $this->WBuyPepperCashAccess->newModifyPepperCash($data);
					if (!$result) {
						$transaction->rollBack();
						return Tool::returnJson([], 416, "提现花椒返款失败");
					}
					// 将冻结的花椒返还给买手
					//修改账号资金
					$pepperCash = number_format($buyAccount['freezing_golds'] - $pepper_cash_record['money'], 2, ".", "");
					if ($pepperCash < 0) {
						$transaction->rollBack();
						return Tool::returnJson([], 488, "买手花椒冻结记录有误");
					}
					$buy_arr = [
						"remail_golds"   => number_format($buyAccount['remail_golds'] + $pepper_cash_record['money'], 2, ".", ""),
						"freezing_golds" => number_format($buyAccount['freezing_golds'] - $pepper_cash_record['money'], 2, ".", ""),
						"buy_id"         => $pepper_cash_record['buy_id'],
					];
					$res     = $this->WBuyUserAcountAccess->freezingGoldsPepper($buy_arr);
					if (!$res) {
						$transaction->rollBack();
						return Tool::returnJson([], 417, "提现花椒返款失败");
					}
					//增加买手提现失败记录
					$buy_cash_arr = [
						"buy_id"        => $pepper_cash_record['buy_id'],
						"serial_num"    => $pepper_cash_record['cash_num'],
						"money"         => $pepper_cash_record['money'],
						"arrival_money" => $pepper_cash_record['cash_money'],
						"account_num"   => $pepper_cash_record['account_num'],
						"username"      => $pepper_cash_record['real_name'],
						"status"        => 2,
						"remark"        => "买手提现花椒返款失败",
						"operator"      => $data['operator'],
					];
					$cash_res     = $this->WBuyPepperCashLogAccess->addBuyPepperCashLog($buy_cash_arr);
					if (!$cash_res) {
						$transaction->rollBack();
						return Tool::returnJson([], 418, "提现花椒失败");
					}
					//增加花椒记录
					$pepper_arra = [
						"buy_id"     => $pepper_cash_record['buy_id'],
						"expend"     => 0,
						"income"     => $pepper_cash_record['money'],
						"balance"    => number_format($buyAccount['remail_golds'] + $pepper_cash_record['money'], 2, ".", ""),
                        "freeze_change" => -$pepper_cash_record['money'],
                        "freeze_balance" => number_format($buyAccount['freezing_golds'] - $pepper_cash_record['money'], 2, ".", ""),
						"remark"     => "买手提现花椒返款失败返还花椒",
						"operatorId" => $data['operator'],
						"type"       => 3,
						"end_user"   => $data['operator'],
					];
					$res_pepper  = $this->WBuyPepperRecordAccess->addBuyPepperRecord($pepper_arra);
					if (!$res_pepper) {
						$transaction->rollBack();
						return Tool::returnJson([], 420, "增加花椒退还记录失败");
					}
					$transaction->commit();
					return Tool::returnJson([], 200, "成功");
				} else {
					$transaction->rollBack();
					return Tool::returnJson([], 422, "操作状态有误");
				}
			}
			//买手本金提现修改备注
			if (1 == $data['role'] && 2 == $data['cash_type']) {
				//获取买手本金记录
				$principal_cash_record = $this->RBuyPrincipalCashAccess->getBuyPrincipalCash($data['id']);
				if (empty($principal_cash_record)) {
					$transaction->rollBack();
					return Tool::returnJson([], 423, "买手本金提现记录不存在");
				}
				if ($principal_cash_record['status'] != 3) {
					$transaction->rollBack();
					return Tool::returnJson([], 423, "该提现状态不能进行返款操作");
				}
				$buyUser = $this->RBuyUserAccess->getBuyUserInfo($principal_cash_record['buy_id']);
				if (empty($buyUser)) {
					$transaction->rollBack();
					return Tool::returnJson([], 425, '提现信息对应的账号信息不存在');
				}
				$buyAccount = $this->RBuyUserAcountAccess->getBuyUserAccount($principal_cash_record['buy_id']);
				if (empty($buyAccount)) {
					$transaction->rollBack();
					return Tool::returnJson([], 426, '买手账号信息异常');
				}

				if (($data['status'] == 4) && empty($data['kq_serial_number'])) {
					$transaction->rollBack();
					return Tool::returnJson([], 401, '交易号不能为空');
				}

				//$result = $this->WBuyPrincipalCashAccess->modifyPepperCash($data);
				$result = $this->WBuyPrincipalCashAccess->newModifyPepperCash($data);
				if (!$result) {
					$transaction->rollBack();
					return Tool::returnJson([], 424, '买手提现本金失败');
				}
				if ($data['status'] == 4) { //成功

					//修改买手本金记录
					$freezing_funds = number_format($buyAccount['freezing_funds'] - $principal_cash_record['money'], 2, ".", "");
					if ($freezing_funds < 0) {
						$transaction->rollBack();
						return Tool::returnJson([], 460, '买手押金数据异常');
					}
					$buy_arr = [
						"remain_funds"   => number_format($buyAccount['remain_funds'], 2, ".", ""),
						"remail_golds"   => $buyAccount['remail_golds'],
						"freezing_funds" => $freezing_funds,
						"freezing_golds" => $buyAccount['freezing_golds'],
						"buy_id"         => $principal_cash_record['buy_id'],
					];
					$buy_res = $this->WBuyUserAcountAccess->modifyBuyUserAccount($buy_arr);
					if (!$buy_res) {
						$transaction->rollBack();
						return Tool::returnJson([], 427, '修改买手账号信息失败');
					}
					//增加买手提现成功记录
					$buy_cash_arr = [
						"buy_id"        => $principal_cash_record['buy_id'],
						"serial_num"    => $principal_cash_record['cash_num'],
						"money"         => $principal_cash_record['money'],
						"arrival_money" => $principal_cash_record['cash_money'],
						"account_num"   => $principal_cash_record['card_num'],
						"username"      => $principal_cash_record['real_name'],
						"status"        => 1,
						"remark"        => "买手提现本金成功",
						"operator"      => $data['operator'],
					];
					$cash_res     = $this->WBuyPrincipalCashLogAccess->addBuyPrincipalCashLog($buy_cash_arr);
					if (!$cash_res) {
						$transaction->rollBack();
						return Tool::returnJson([], 428, "提现本金失败");
					}

					// 修改订单状态
					if ($principal_cash_record) {
						$ids = explode(',', $principal_cash_record['task_order_ids']);
						foreach ($ids as $val) {
							$editData = [
								'order_status'  => 13,
								'task_order_id' => $val,
							];
							$this->WTaskOrderAccess->confirmRefunds($editData);
						}
					}

					$transaction->commit();
					return Tool::returnJson([], 200, "成功");
				} else if ($data['status'] == 5)//返款失败
				{
					//修改买手本金记录
					$freezing_funds = number_format($buyAccount['freezing_funds'] - $principal_cash_record['money'], 2, ".", "");
					if ($freezing_funds < 0) {
						$transaction->rollBack();
						return Tool::returnJson([], 480, "买手账号异常");
					}

					$buy_arr = [
						"remain_funds"   => number_format($buyAccount['remain_funds'] + $principal_cash_record['money'], 2, ".", ""),
						"remail_golds"   => number_format($buyAccount['remail_golds'], 2, ".", ""),
						"freezing_funds" => number_format($buyAccount['freezing_funds'] - $principal_cash_record['money'], 2, ".", ""),
						"freezing_golds" => $buyAccount['freezing_golds'],
						"buy_id"         => $principal_cash_record['buy_id'],
					];
					$buy_res = $this->WBuyUserAcountAccess->modifyBuyUserAccount($buy_arr);
					if (!$buy_res) {
						$transaction->rollBack();
						return Tool::returnJson([], 429, '修改买手账号信息失败');
					}
					//
					//增加买手提现失败记录
					$buy_cash_arr  = [
						"buy_id"        => $principal_cash_record['buy_id'],
						"serial_num"    => $principal_cash_record['cash_num'],
						"money"         => $principal_cash_record['money'],
						"arrival_money" => $principal_cash_record['cash_money'],
						"account_num"   => $principal_cash_record['card_num'],
						"username"      => $principal_cash_record['real_name'],
						"status"        => 2,
						"remark"        => "买手提现本金返款失败",
						"operator"      => $data['operator'],
					];
					$principal_res = $this->WBuyPrincipalCashLogAccess->addBuyPrincipalCashLog($buy_cash_arr);
					if (!$principal_res) {
						$transaction->rollBack();
						return Tool::returnJson([], 430, "提现本金失败");

					}
					//买手子订单状态回退
					$task_order_res = $this->WTaskOrderAccess->TaskOrderCancelStatus(['task_order_ids' => explode(",", $principal_cash_record['task_order_ids']), "order_status" => 8]);
					if (!$task_order_res) {
						$transaction->rollBack();
						return Tool::returnJson([], 4333, "子任务订单状态回退失败");
					}

					$transaction->commit();
					return Tool::returnJson([], 200, "成功");
				} else {
					$transaction->rollBack();
					return Tool::returnJson([], 432, "操作类型有误");
				}

			}
			//商家本金提现修改备注
			if (2 == $data['role'] && 2 == $data['cash_type']) {
				//获取商家提现本金记录
				$bus_cash_record = $this->RBusinessCashAccess->getBusinessCashRecord($data['id']);
				if (empty($bus_cash_record)) {
					$transaction->rollBack();
					return Tool::returnJson([], 433, "商家提现记录不存在");
				}
				if ($bus_cash_record['cash_status'] != 3) {
					$transaction->rollBack();
					return Tool::returnJson([], 435, "该提现记录不能操作");
				}
				$busUser = $this->RBuyUserAccess->getBuyUserInfo($bus_cash_record['bus_id']);
				if (empty($busUser)) {
					$transaction->rollBack();
					return Tool::returnJson([], 436, '提现信息对应的账号信息不存在');
				}
				$businessAccount = $this->RBusinessAccountAccess->getBusinessAccount($bus_cash_record['bus_id']);
				if (empty($businessAccount)) {
					$transaction->rollBack();
					return Tool::returnJson([], 437, '商家账号信息异常');
				}

				if (($data['status'] == 4) && empty($data['kq_serial_number'])) {
					$transaction->rollBack();
					return Tool::returnJson([], 401, '交易号不能为空');
				}

				//$result = $this->WBusinessCashAccess->modifyCashRecord($data);
				$result = $this->WBusinessCashAccess->endData($data);
				if (!$result) {
					$transaction->rollBack();
					return Tool::returnJson([], 438, "提现本金记录失败");
				}

				//提现成功
				if ($data['status'] == 4) {
					if (empty($data['kq_serial_number'])) {
						$transaction->rollBack();
						return Tool::returnJson([], 401, '交易号不能为空');
					}
					//修改商家押金和提现记录
					$freezing_deposit = number_format($businessAccount['freezing_deposit'], 2, ".", "");
					$cash_deposit     = number_format($businessAccount['cash_deposit'] - $bus_cash_record['money'], 2, ".", "");
					if ($cash_deposit < 0) {
						$transaction->rollBack();
						return Tool::returnJson([], 439, "商家账号资金异常");
					}

					$bus_arr = [
						"bus_id"           => $bus_cash_record['bus_id'],
						"total_deposit"    => number_format($businessAccount['total_deposit'] - $bus_cash_record['money'], 2, ".", ""),
						"ava_deposit"      => $businessAccount['ava_deposit'],
						"freezing_deposit" => $freezing_deposit,
						"ava_pepper"       => $businessAccount['ava_pepper'],
						"freezing_golds"   => $businessAccount['freezing_golds'],
						"cash_deposit"     => $cash_deposit,
					];
					$bus_res = $this->WBusinessAcountAccess->modifyBusAccountCash($bus_arr);
					if (!$bus_res) {
						$transaction->rollBack();
						return Tool::returnJson([], 440, "修改商家账号信息失败");
					}
					//增加商家提现成功记录
					$bus_cash_arr = [
						"bus_id"        => $bus_cash_record['bus_id'],
						"serial_num"    => $bus_cash_record['serial_num'],
						"money"         => $bus_cash_record['money'],
						"arrival_money" => $bus_cash_record['arrival_money'],
						"account_num"   => $bus_cash_record['account_num'],
						"username"      => $bus_cash_record['username'],
						"status"        => 1,
						"remark"        => "商家提现本金成功",
						"operator"      => $data['operator'],
					];
					$bus_cash_res = $this->WBusCashLogAccess->addBusCashLog($bus_cash_arr);
					if (!$bus_cash_res) {
						$transaction->rollBack();
						return Tool::returnJson([], 441, "提现本金失败");

					}
					$transaction->commit();
					return Tool::returnJson([], 200, "成功");

				} else if ($data['status'] == 5) { //提现失败

						//修改商家押金和提现记录
						$total_deposit    = number_format($businessAccount['total_deposit'], 2, ".", "");
						$ava_deposit      = number_format($businessAccount['ava_deposit'] + $bus_cash_record['money'], 2, ".", "");
						$freezing_deposit = number_format($businessAccount['freezing_deposit'], 2, ".", "");
						$cash_deposit     = number_format($businessAccount['cash_deposit'] - $bus_cash_record['money'], 2, ".", "");
						if ($freezing_deposit < 0 || $cash_deposit < 0) {
							$transaction->rollBack();
							return Tool::returnJson([], 442, "商家账号资金异常");
						}

						$bus_arr = [
							"bus_id"           => $bus_cash_record['bus_id'],
							"total_deposit"    => $total_deposit,
							"ava_deposit"      => $ava_deposit,
							"freezing_deposit" => $freezing_deposit,
							"ava_pepper"       => $businessAccount['ava_pepper'],
							"freezing_golds"   => $businessAccount['freezing_golds'],
							"cash_deposit"     => $cash_deposit,
						];
						$bus_res = $this->WBusinessAcountAccess->modifyBusAccountCash($bus_arr);
						if (!$bus_res) {
							$transaction->rollBack();
							return Tool::returnJson([], 443, "修改商家账号信息失败");
						}
						//增加商家押金日志
						$bus_deposit  = [
							"bus_id"     => $bus_cash_record['bus_id'],
							"type"       => 3,
							"expend"     => 0,
							"income"     => $bus_cash_record['money'],
							"balance"    => $ava_deposit,
                            "freeze_change" =>-$bus_cash_record['money'],
                            "freeze_balance" => number_format($businessAccount['cash_deposit'] - $bus_cash_record['money'], 2, ".", ""),
							"remark"     => "商家提现失败返还押金",
							"operatorId" => $data['operator'],
							"end_user"   => $data['operator'],
						];
						$deposite_res = $this->WBusinessDepositRecordAccess->addDepositRecord($bus_deposit);
						if (!$deposite_res) {
							$transaction->rollBack();
							return Tool::returnJson([], 445, "增加押金记录失败");
						}
						//增加商家提现成功记录
						$bus_cash_arr = [
							"bus_id"        => $bus_cash_record['bus_id'],
							"serial_num"    => $bus_cash_record['serial_num'],
							"money"         => $bus_cash_record['money'],
							"arrival_money" => $bus_cash_record['arrival_money'],
							"account_num"   => $bus_cash_record['account_num'],
							"username"      => $bus_cash_record['username'],
							"status"        => 2,
							"remark"        => "商家提现本金失败",
							"operator"      => $data['operator'],
						];
						$bus_cash_res = $this->WBusCashLogAccess->addBusCashLog($bus_cash_arr);
						if (!$bus_cash_res) {
							$transaction->rollBack();
							return Tool::returnJson([], 446, "提现本金失败");

						}
						$transaction->commit();
						return Tool::returnJson([], 200, "成功");
					} else {
						$transaction->rollBack();
						return Tool::returnJson([], 447, "提现本金记录失败");
					}
			}

			$transaction->rollBack();
			return Tool::returnJson([], 450, "你的参数有误");
		} catch (\Exception $e) {
			$transaction->rollBack();
			return Tool::returnJson([], 5000, "异常失败" . $e->getMessage());
		}
	}


	/**
	 * memberDetail 用户资金明细
	 * @param $data
	 * @author 中崋 (1291713293@qq.com)
	 * @date   2019/5/10
	 * @return array
	 */
	public function memberDetail($data) {
		if (empty($data['role'])) {
			return Tool::returnJson([], 401, "用户类型不能为空");
		}
		if (empty($data['cash_type'])) {
			return Tool::returnJson([], 401, "提现类型不能为空");
		}

		//排序
		$orderBy = "create_time desc";
		if ($data['sort']) {
			$orderBy = " create_time  " . $data['sort'];
		}

		//条件
		$where = '1=1';
		if ($data['phone']) {
			$where = ' u.phone = '.$data['phone'];
		}
		if ($data['begin_time']) {
			$where = ' u.create_time > '.$data['begin_time'];
		}
		if ($data['end_time']) {
			$where = ' u.create_time < '.$data['end_time'];
		}


		$list = [];

		if (($data['role'] == 1) && ($data['cash_type'] == 1)) {
			// 买手本金

			$list = $this->RBuyDepositRecordAccess->getPageList($where, $orderBy, $data['limit']);

		} else if (($data['role'] == 1) && ($data['cash_type'] == 2)) {
			//买手佣金
			$list = $this->RBuyPepperRecordAccess->getPageList($where, $orderBy, $data['limit']);

		} else if (($data['role'] == 2) && ($data['cash_type'] == 1)) {
			//商家押金
			$list = $this->RBusinessDepositRecordAccess->getPageList($where, $orderBy, $data['limit']);

		} else if (($data['role'] == 2) && ($data['cash_type'] == 2)) {
			//商家花椒
			$list = $this->RBusinessPepperRecordAccess->getPageList($where, $orderBy, $data['limit'], $data['page_num']);
		}

	/*	var_dump($data);
		var_dump($list);
		exit();*/
		return Tool::returnJson($list, 200, "获取成功！");
	}

}